I have a list of services defined in my Consul catalog, and I would like to remove the ones that have no label defined.
This list of services looks like this:
{
"json": {
"consul": [],
"consul-exporter": [],
"consul-8600": [
"traefik.enable=false",
"udp"
],
"snmp-gateway": [],
}
}
I would like to filter it using JMESPath to have the result containing only
{
"json": {
"consul-8600": [
"traefik.enable=false",
"udp"
],
}
}
But the syntax of JMESPath filtering remains obscure to me.
I think I should use the length
function to get the size of the attributes array, but how?
So far, I have a json.[length(*)>0]
filter, but it shows no value.
What should I change to have a non-null result?
In Ansible 2.5 and later:
It is possible by combining JMESPath query with Ansible dict2items
filter and Jinja2 dict
function:
- debug:
msg: "{{ dict(json | dict2items | json_query('@[?value].[key, value]')) }}"