i have complex result in JSON format and struggling to extract values out of it. I tested the JSONpath online evaluator https://jsonpath.com/ where i can just simply type
$.[*].toPort
and get the list of requested variables.
I've tried to use the same syntax in Ansible but getting empty list.
- name: JSON test
hosts: localhost
gather_facts: no
vars:
jsoncontent:
{
"infraAccPortP": {
"attributes": {
"annotation": "",
"childAction": "",
"descr": "",
"nameAlias": "",
"ownerKey": "",
"ownerTag": "",
"status": "",
"uid": "15374"
},
"children": [
{
"infraHPortS": {
"attributes": {
"annotation": "",
"uid": "8927"
},
"children": [
{
"infraPortBlk": {
"attributes": {
"fromPort": "41",
"toPort": "41",
"uid": "8927"
}
}
}
]
}
},
{
"infraHPortS": {
"attributes": {
"annotation": "",
"uid": "8927"
},
"children": [
{
"infraPortBlk": {
"attributes": {
"fromPort": "42",
"toPort": "42",
"uid": "8927"
}
}
}
]
}
}
]
}
}
tasks:
- name: show jsoncontent
debug:
var: jsoncontent
- name: Show just toPort values
debug:
msg: "{{ jsoncontent | json_query(jmesquery) }}"
vars:
jmesquery: "[*].toPort"
Any help how to adjust the query to get the expected result? Its bit frustrating as there is not much documentation on how json_query is implemented in Ansible