# Collecting key/value from json with ansible

**URL:** <https://forum.ansible.com/t/collecting-key-value-from-json-with-ansible/35381>\
**Category:** Ansible Project\
**Created:** [June 21, 2021, 11:57am UTC](https://forum.ansible.com/t/collecting-key-value-from-json-with-ansible/35381 "2021-06-21T11:57:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rodrigues](https://avatars.discourse-cdn.com/v4/letter/r/8797f3/32.png) [@Rodrigues](https://forum.ansible.com/u/Rodrigues)\
**Post date:** [June 21, 2021, 11:57am UTC](https://forum.ansible.com/t/collecting-key-value-from-json-with-ansible/35381/1 "2021-06-21T11:57:14Z")

</div>

Hi all,

I have the below json data, however i’m having a hard time collecting the two keys i’m interested. which is parent\_path and id

I can get to a single value using this, however i’d like to loop through all of items that start with “nsx-”

- name: Show data2  
debug:  
msg: “{{ tresult.results[4].json.results[0].id }} {{ tresult.results[4].json.results[0].parent\_path }}”  
delegate\_to: localhost

{  
“msg”: {  
“ansible\_loop\_var”: “item”,  
“cache\_control”: “no-cache, no-store, max-age=0, must-revalidate”,  
“changed”: false,  
“connection”: “close”,  
“content”: “{\n "results" : [{\n "mac\_discovery\_profile\_path" : "/infra/mac-discovery-profiles/Allow-MacDiscoveryProfile",\n "ip\_discovery\_profile\_path" : "/infra/ip-discovery-profiles/default-ip-discovery-profile",\n "resource\_type" : "SegmentDiscoveryProfileBindingMap",\n "id" : "e361a6f8-88a7-47f0-be00-a7fb412e8f56",\n "display\_name" : "e361a6f8-88a7-47f0-be00-a7fb412e8f56",\n "path" : "/infra/segments/nsx-Management—10.26.170.0\_24/segment-discovery-profile-binding-maps/e361a6f8-88a7-47f0-be00-a7fb412e8f56",\n "relative\_path" : "e361a6f8-88a7-47f0-be00-a7fb412e8f56",\n "parent\_path" : "/infra/segments/nsx-Management—10.26.170.0\_24",\n "unique\_id" : "ce6235a7-d960-4407-8d2c-cf110197ca4a",\n "marked\_for\_delete" : false,\n "overridden" : false,\n "\_create\_user" : "admin",\n "\_create\_time" : 1623945358415,\n "\_last\_modified\_user" : "admin",\n "\_last\_modified\_time" : 1623947366813,\n "\_system\_owned" : false,\n "\_protection" : "NOT\_PROTECTED",\n "\_revision" : 1\n }],\n "result\_count" : 1,\n "sort\_by" : "display\_name",\n "sort\_ascending" : true\n}”,  
“content\_type”: “application/json”,  
“cookies”: {  
“JSESSIONID”: “7D0E069A624336C3E6782E112A5A8B73”  
},  
“cookies\_string”: “JSESSIONID=7D0E069A624336C3E6782E112A5A8B73”,  
“date”: “Mon, 21 Jun 2021 11:28:20 GMT”,  
“elapsed”: 0,  
“expires”: “0”,  
“failed”: false,  
“invocation”: {  
“module\_args”: {  
“attributes”: null,  
“body”: null,  
“body\_format”: “json”,  
“client\_cert”: null,  
“client\_key”: null,  
“creates”: null,  
“dest”: null,  
“follow\_redirects”: “none”,  
“force”: false,  
“force\_basic\_auth”: true,  
“group”: null,  
“headers”: {  
“Content-Type”: “application/json”  
},  
“http\_agent”: “ansible-httpget”,  
“method”: “GET”,  
“mode”: null,  
“owner”: null,  
“password”: “VALUE\_SPECIFIED\_IN\_NO\_LOG\_PARAMETER”,  
“remote\_src”: false,  
“removes”: null,  
“return\_content”: true,  
“selevel”: null,  
“serole”: null,  
“setype”: null,  
“seuser”: null,  
“src”: null,  
“status\_code”: [  
“200”  
],  
“timeout”: 30,  
“unix\_socket”: null,  
“unsafe\_writes”: false,  
“url”: “[https://nsx-dev-Ork2.infra.domain.local/policy/api/v1/infra/segments/nsx-Management---10.26.170.0\_24/segment-discovery-profile-binding-maps](https://nsx-dev-Ork2.infra.domain.local/policy/api/v1/infra/segments/nsx-Management---10.26.170.0_24/segment-discovery-profile-binding-maps)”,  
“url\_password”: “VALUE\_SPECIFIED\_IN\_NO\_LOG\_PARAMETER”,  
“url\_username”: “admin”,  
“use\_proxy”: true,  
“user”: “admin”,  
“validate\_certs”: false  
}  
},  
“item”: {  
“description”: null,  
“dvswitch\_name”: “DVS-Dev-Ork2”,  
“key”: “dvportgroup-13”,  
“network\_policy”: {  
“forged\_transmits”: false,  
“mac\_changes”: false,  
“promiscuous”: false  
},  
“num\_ports”: 15,  
“port\_policy”: {  
“block\_override”: true,  
“ipfix\_override”: false,  
“live\_port\_move”: false,  
“network\_rp\_override”: false,  
“port\_config\_reset\_at\_disconnect”: true,  
“security\_override”: false,  
“shaping\_override”: false,  
“traffic\_filter\_override”: false,  
“uplink\_teaming\_override”: false,  
“vendor\_config\_override”: false,  
“vlan\_override”: false  
},  
“portgroup\_name”: “Management - 10.26.170.0\_24”,  
“teaming\_policy”: {  
“inbound\_policy”: true,  
“notify\_switches”: true,  
“policy”: “loadbalance\_ip”,  
“rolling\_order”: false  
},  
“type”: “earlyBinding”,  
“vlan\_info”: {  
“pvlan”: false,  
“trunk”: false,  
“vlan\_id”: “0”  
}  
},  
“json”: {  
“result\_count”: 1,  
“results”: [  
{  
“\_create\_time”: 1623945358415,  
“\_create\_user”: “admin”,  
“\_last\_modified\_time”: 1623947366813,  
“\_last\_modified\_user”: “admin”,  
“\_protection”: “NOT\_PROTECTED”,  
“\_revision”: 1,  
“\_system\_owned”: false,  
“display\_name”: “e361a6f8-88a7-47f0-be00-a7fb412e8f56”,  
“id”: “e361a6f8-88a7-47f0-be00-a7fb412e8f56”,  
“ip\_discovery\_profile\_path”: “/infra/ip-discovery-profiles/default-ip-discovery-profile”,  
“mac\_discovery\_profile\_path”: “/infra/mac-discovery-profiles/Allow-MacDiscoveryProfile”,  
“marked\_for\_delete”: false,  
“overridden”: false,  
“parent\_path”: “/infra/segments/nsx-Management—10.26.170.0\_24”,  
“path”: “/infra/segments/nsx-Management—10.26.170.0\_24/segment-discovery-profile-binding-maps/e361a6f8-88a7-47f0-be00-a7fb412e8f56”,  
“relative\_path”: “e361a6f8-88a7-47f0-be00-a7fb412e8f56”,  
“resource\_type”: “SegmentDiscoveryProfileBindingMap”,  
“unique\_id”: “ce6235a7-d960-4407-8d2c-cf110197ca4a”  
}  
],  
“sort\_ascending”: true,  
“sort\_by”: “display\_name”  
},  
“msg”: “OK (unknown bytes)”,  
“pragma”: “no-cache”,  
“redirected”: false,  
“server”: “NSX”,  
“set\_cookie”: “JSESSIONID=7D0E069A624336C3E6782E112A5A8B73; Path=/; Secure; HttpOnly”,  
“status”: 200,  
“strict\_transport\_security”: “max-age=31536000 ; includeSubDomains”,  
“transfer\_encoding”: “chunked”,  
“url”: “[https://nsx-dev-Ork2.infra.domain.local/policy/api/v1/infra/segments/nsx-Management---10.26.170.0\_24/segment-discovery-profile-binding-maps](https://nsx-dev-Ork2.infra.domain.local/policy/api/v1/infra/segments/nsx-Management---10.26.170.0_24/segment-discovery-profile-binding-maps)”,  
“vary”: “accept-encoding”,  
“x\_content\_type\_options”: “nosniff”,  
“x\_frame\_options”: “SAMEORIGIN”,  
“x\_nsx\_requestid”: “db6a3c47-a75d-43f3-a8d0-e9b91acebc63”,  
“x\_xss\_protection”: “1; mode=block”  
}  
}
