-
Hi I am trying to select all objects within an array, but some objects does not have all attributes. I would expect that this simple query returns first array element, while it returns nothing. Can I filter based on existence of an attribute ? |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 2 replies
-
There is something strange going on with the error output that I've been meaning to look into, but this would be throwing an error when it see's an object without the name. You can get this working by adding a echo '{ "users": [ { "name": "Tom" }, { "wrong_name": "Jim" } ] }' | dasel -r json 'users.all().name?'
"Tom" This is documented here: https://daseldocs.tomwright.me/functions/property
|
Beta Was this translation helpful? Give feedback.
-
Yeah, lack of error is misleading (it was Dasel 2.2).
|
Beta Was this translation helpful? Give feedback.
-
So the proper object selection is:
I would not find this without your help. Thank you. |
Beta Was this translation helpful? Give feedback.
-
Any idea how to reverse the search (find objects that does not have a property) ? This almost work :
|
Beta Was this translation helpful? Give feedback.
-
Note that some of the following examples will only work with dasel v2.3.4 and up due to some bugs that have been fixed. This shows you users that have a name property that is not blank. $ echo '{
"users": [
{
"name": "Tom"
},
{
"name": "false"
},
{
"name": true
},
{
"wrong_name": "Jim"
},
{
"name": ""
}
]
}' | dasel -r json 'users.all().filter(name?.len())'
{
"name": "Tom"
}
{
"name": "false"
}
{
"name": true
} This shows you users that have no name field, or a blank name field. $ echo '{
"users": [
{
"name": "Tom"
},
{
"name": "false"
},
{
"name": true
},
{
"wrong_name": "Jim"
},
{
"name": ""
}
]
}' | dasel -r json 'users.all().filter(not(name?.len()))'
{
"wrong_name": "Jim"
}
{
"name": ""
} This shows you users that have a name field, regardless of whether it is blank or not. $ echo '{
"users": [
{
"name": "Tom"
},
{
"name": "false"
},
{
"name": true
},
{
"wrong_name": "Jim"
},
{
"name": ""
}
]
}' | dasel -r json 'users.all().filter(keys().all().filter(equal(.,name)))'
{
"name": "Tom"
}
{
"name": "false"
}
{
"name": true
}
{
"name": ""
} This shows you users that do NOT have a name field, regardless of whether it is blank or not. $ echo '{
"users": [
{
"name": "Tom"
},
{
"name": "false"
},
{
"name": true
},
{
"wrong_name": "Jim"
},
{
"name": ""
}
]
}' | dasel -r json 'users.all().filter(not(keys().all().filter(equal(.,name)).len()))'
{
"wrong_name": "Jim"
} |
Beta Was this translation helpful? Give feedback.
-
Why
What about defining a new function "isKey(x)" = keys().all().filter(equal(.,x)), so the example will become: BTW. The Equal documentation page uses direct Go invocation in the example. |
Beta Was this translation helpful? Give feedback.
There is something strange going on with the error output that I've been meaning to look into, but this would be throwing an error when it see's an object without the name.
You can get this working by adding a
?
after the "optional" property name, e.g:This is documented here: https://daseldocs.tomwright.me/functions/property