I'm trying to filter properties of an object in jmespath based on the value of a subproperty and want to include only those properties where the subproperty is set to a specific value.
Based on this example data:
{
"a": {
"feature": {
"enabled": true,
}
},
"b": {
},
"c": {
"feature": {
"enabled": false
}
}
}
I'd like to get an object with all properties where the feature is enabled.
{
"a": {
"feature": {
"enabled": true,
}
}
}
I figured I could use this jmespath query to filter the objects where property. enabled
is set to true. Unfortunateley, it doesn't seem to work and instead returns an empty array.
*[?feature.enabled==`true`]
*.feature.enabled
or *[feature.enabled]
return just the boolean values without any context.
Even if *[?feature.enabled==
true]
would work, it would just be an array of the property values, but I need the keys (a
and c
) aswell. Is there any way to make this happen in jmespath?
This is all part of an ansible playbook, so there would certainly be a way to achieve selection in a different way (Jinja2 templates or custom plugin) but I wanted to try jmespath and would reason, that it should be capable of such a task.
Sorry, but AFAIK this is impossible in native JMESPath.
There are custom built-in functions for this purpose in different tools like
to_entries
in jq.For
jmespath.py
and thus for Ansible there is hanging pull request to implement keys manipulation.Update: I've made a patched version of json_query filter.
See this answer for additional info.