# Help with complex json\_query

**URL:** <https://forum.ansible.com/t/help-with-complex-json-query/36944>\
**Category:** Ansible Project\
**Created:** [August 3, 2022, 9:29pm UTC](https://forum.ansible.com/t/help-with-complex-json-query/36944 "2022-08-03T21:29:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spiro\_Mitsialis](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@Spiro\_Mitsialis](https://forum.ansible.com/u/Spiro_Mitsialis)\
**Post date:** [August 3, 2022, 9:29pm UTC](https://forum.ansible.com/t/help-with-complex-json-query/36944/1 "2022-08-03T21:29:27Z")

</div>

Hi,

I have the following data:  
“application\_profiles”: [  
{  
“app\_desc”: “”,  
“app\_name”: “NCS\_Net\_inf”,  
“epg\_name”: [  
“SPAN-destination-analysis”,  
“infra\_auth\_servers”,  
“netinf\_test\_servers”,  
“paaa-infra-mgt”,  
“vl2003”  
]  
},

{  
“app\_desc”: “dept: MedIT”,  
“app\_name”: “faculty\_of\_medicine”,  
“epg\_name”: [  
“medit\_jump\_server”  
]  
},

{  
“app\_desc”: “”,  
“app\_name”: “dc\_qa\_app\_fe”,  
“epg\_name”: [  
“vl3800”  
]  
}

]  
}

AND

“epg\_bd\_list”: [  
{  
“fvAEPg”: {  
“attributes”: {  
“name”: “SPAN-destination-analysis”,  
“nameAlias”: “vl2022”  
},

{  
“fvAEPg”: {  
“attributes”: {

“name”: “vl72”,  
“nameAlias”: “Bone\_Centre”

}  
]

I’m trying to do a json\_query using the contain function but I cannot seem to figure out how to use it properly.

- debug:

msg: “{{ item.fvAEPg.attributes.name }} is in app profile: {{ application\_profiles | to\_json | from\_json | json\_query(‘\*.[?epg\_name.contains(@, `{{ item.fvAEPg.attributes.name }}`)].app\_name’) }}”  
loop: “{{ epg\_bd\_list }}”

I’ve done a lot of json\_query but this is the first time that the variable I am searching for “epg\_name” is a list and not a single item. I managed to make it work if the matching “item.fvAEPg.attributes.name” is the first item in the list. I’d like to pick out the app\_name if the item.fvAEPg.attributes.name is in the list of epg\_name.

Thanks,  
Spiro

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [August 4, 2022, 4:26am UTC](https://forum.ansible.com/t/help-with-complex-json-query/36944/2 "2022-08-04T04:26:30Z")

</div>

If you have to resort to json\_query, you might as well create real loops. Below is the playbook I used to explore your data. I left in some experiments that helped me along the way. Perhaps something here will be useful to you. I added another element to your epg\_bd\_list to cover the single element case.

```auto
---
- name: json_query playground
  hosts: localhost
  gather_facts: no
  vars:
    application_profiles:
      - app_desc: ""
        app_name: NCS_Net_inf
        epg_name:
           - SPAN-destination-analysis
           - infra_auth_servers
           - netinf_test_servers
           - paaa-infra-mgt
           - vl2003
      - app_desc: "dept: MedIT"
        app_name: faculty_of_medicine
        epg_name:
           - medit_jump_server
      - app_desc: ""
        app_name: dc_qa_app_fe
        epg_name:
           - vl3800
    epg_bd_list:
      - fvAEPg:
          attributes:
            name: SPAN-destination-analysis
            nameAlias: vl2022
      - fvAEPg:
          attributes:
            name: vl72
            nameAlias: Bone_Centre
      - fvAEPg:
          attributes:
            name: medit_jump_server
            nameAlias: Jumpin_Jiminy
  tasks:
    - name: Explorations of ways to examine epg_bd_list
      debug:
        msg:
         - "aaa {{ epg_bd_list | map(attribute='fvAEPg') }}"
         - "bbb {{ epg_bd_list | map(attribute='fvAEPg.attributes.name') }}"
         - "ccc {{ epg_bd_list | map('dict2items') }}"
         - "ddd {{ epg_bd_list | map('dict2items') | flatten | map(attribute='value.attributes.name') }}"

    - name: isolate the app_name from application_profiles
      debug:
        msg:
         - "aaa {{ application_profiles | map(attribute='app_name') }}"

    - name: Uses product of epg_bd_lst.attributes.name and application_profiles to get matching app_names
      debug:
        msg:
         - |
          {% set app_names=[] %}
          {% for app in application_profiles | product(epg_bd_list| map('dict2items') | flatten | map(attribute='value.attributes.name')) %}
          {% if app[1] in app[0]['epg_name'] %}
          {% set _ = app_names.append( app[0]['app_name'] ) %}
          {% endif %}
          {% endfor %}
          {{ app_names }}

# - name: Would this ever be handy? Maybe
# debug:
# msg: "{{ item }}: {{ application_profiles | map(attribute='epg_name') }}"
# loop: "{{ epg_bd_list | map('dict2items') | flatten | map(attribute='value.attributes.name') }}"
#
# - name: How about this?
# debug:
# msg: "{{ application_profiles | map(attribute='epg_name') | map('select', 'in', item) }}"
# loop: "{{ epg_bd_list | map('dict2items') | flatten | map(attribute='value.attributes.name') }}"

```

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 4, 2022, 4:51am UTC](https://forum.ansible.com/t/help-with-complex-json-query/36944/3 "2022-08-04T04:51:41Z")

</div>

Try this:

&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ \_item }} is in app profile: {{ app\_profile }}"  
&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ epg\_bd\_list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_item: "{{ item.fvAEPg.attributes.name }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_query: '[?epg\_name.contains(@, `{{ _item }}`)].app\_name'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app\_profile: "{{ application\_profiles|json\_query(\_query) }}"

Note: In the JSON, the braces {} are not properly balanced. Use YAML

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;application\_profiles:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- app\_desc: ''  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app\_name: NCS\_Net\_inf  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;epg\_name:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- SPAN-destination-analysis  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- infra\_auth\_servers  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- netinf\_test\_servers  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- paaa-infra-mgt  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- vl2003  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- app\_desc: 'dept: MedIT'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app\_name: faculty\_of\_medicine  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;epg\_name:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- medit\_jump\_server  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- app\_desc: ''  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app\_name: dc\_qa\_app\_fe  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;epg\_name:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- vl3800

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;epg\_bd\_list:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- fvAEPg:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attributes:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: SPAN-destination-analysis  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameAlias: vl2022  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- fvAEPg:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attributes:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: vl72  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nameAlias: Bone\_Centre
