# json filter

**URL:** <https://forum.ansible.com/t/json-filter/30298>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [May 10, 2019, 2:42am UTC](https://forum.ansible.com/t/json-filter/30298 "2019-05-10T02:42:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicholas\_Britton](https://avatars.discourse-cdn.com/v4/letter/n/a587f6/32.png) [@Nicholas\_Britton](https://forum.ansible.com/u/Nicholas_Britton)\
**Post date:** [May 10, 2019, 2:42am UTC](https://forum.ansible.com/t/json-filter/30298/1 "2019-05-10T02:42:13Z")

</div>

I have been trying to work with the filter for json responces but i am not having much luck. I can print out a line of intrest, but i am not able to iteriate over the other objects in the response when i try to loop through it.

Here is the response i get from the uri call :

`  
TASK [debug var assets] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [127.0.0.1] =\> {  
“msg”: {  
“data”: [  
{  
“attributes”: {  
“assetLastBackupStatuses”: ,  
“assetSubscriptions”: ,  
“assetType”: “Virtual Machine”,  
“description”: “Owner - name, Project: IDM”,  
“discoveryHosts”: [  
{  
“hostName”: “server01”,  
“ipAddress”: “x.x.x.x”,  
“uuid”: “someuid”  
}  
],  
“displayName”: “[server01.domain.com](http://server01.domain.com)”,  
“extendedAttributes”: {  
“annotation”: “Owner - Name, Project: IDM”,  
“cluster”: “Cluster01”,  
“connectionState”: “connected”,  
“datacenter”: “DC01”,  
“datastore”: “DS01”,  
“displayName”: “[server01.domain.com](http://server01.domain.com)”,  
“dnsName”: “[server01.domain.com](http://server01.domain.com)”,  
“guestId”: “windows8Server64Guest”,  
“host”: “[host01.domain.com](http://host01.domain.com)”,  
“hostName”: “x.x.x.x”,  
“hostPath”: “Engineering Resource”,  
“instanceUuid”: “someuuid”,  
“ipAddress”: x.x.x.x",  
“mor”: “vm-921”,  
“network”: “dvPortGroup\_vLAN##”,  
“nicIpaddresses”: “(x.x.x.x)”,  
“path”: “Engineering Resource”,  
“powerState”: “poweredOn”,  
“snapshotDir”: “DS01”,  
“template”: “false”,  
“uuid”: “someuuid”,  
“vCenter”: “[vcenter.domain.com](http://vcenter.domain.com)”,  
“vCenterVersion”: “5.5.0”,  
“version”: “vmx-08”,  
“vmAbsolutePath”: “/vcenter01.domain.com/Engineering/Engineering Resource/host.domain.com”,  
“vmxDir”: “DS01”  
},  
“firstDiscoveredTime”: “2018-09-26T14:14:30.775Z”,  
“lastDiscoveredTime”: “2019-05-09T19:26:52.950Z”,  
“masters”: [  
{  
“hostName”: “nbumaster01”,  
“ipAddress”: “x.x.x.x”,  
“uuid”: “someuid”  
}  
],  
“protectionMethods”: ,  
“providerGeneratedId”: “VMware\_5001b01e-651c-f63c-3b24-e279fb1b8fd4”,  
“version”: “5.5.0”,  
“workloadType”: “VMware”  
},  
“id”: “d329683a-1af7-4a42-b2e5-afd34f9d693a”,  
“links”: {  
“self”: {  
“href”: “/assets/d329683a-1af7-4a42-b2e5-afd34f9d693a”  
}  
},  
“relationships”: {  
“assetGroups”: {  
“data”: ,  
“links”: {  
“self”: {  
“href”: “/assets/d329683a-1af7-4a42-b2e5-afd34f9d693a/asset-groups”  
}  
}  
}  
},  
“type”: “asset”  
},  
{

`

Here is the code i am working with so far:

I have tried using multiple variants, but i have not had any luck.

`

- name : debug var assets  
debug:  
msg: “{{assets.json}}”
- name : debug json value  
debug:  
msg: “{{assets.json.data[0].attributes.assetType}}”
- name : debug json value  
debug:  
msg: “{{assets.json.data[0].attributes.displayName}}”
- name: display all displaynames  
debug:  
var: item  
loop: “{{assets.json | json\_query(‘attributes.displayName’)}}”

`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 13, 2019, 2:14pm UTC](https://forum.ansible.com/t/json-filter/30298/2 "2019-05-13T14:14:48Z")

</div>

When something 'is json' it is a string with content that is supposed  
to be JSON parsable, so you cannot access the structure directly, you  
need to convert it firs via from\_json filtert:

&nbsp;&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{(assets.json|from\_json).data[0].attributes.assetType}}"
