# loop through a json object

**URL:** <https://forum.ansible.com/t/loop-through-a-json-object/31115>\
**Category:** Ansible Project\
**Created:** [August 23, 2019, 8:22pm UTC](https://forum.ansible.com/t/loop-through-a-json-object/31115 "2019-08-23T20:22:58Z")\
**Posts on this page:** 10\
**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:** [August 23, 2019, 8:22pm UTC](https://forum.ansible.com/t/loop-through-a-json-object/31115/1 "2019-08-23T20:22:58Z")

</div>

I am trying to loop through a json object that is returned and list out key pieces of information, or later on do something with these key peices of information from each object. I have been trying to do this with JMESpath examples but have not had much luck. Would anyone be able to help point me in the right direction.

For this example i am trying to pull out hostGroup.hostGroupdId, hostGroup.hostgroupname, ldev.ldevid, lun.lun, lun.lunid

Here is a sample of the output returned from json\_out.json:

`  
TASK [debug lunpaths] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [127.0.0.1] =\> {  
“msg”: {  
“count”: 4,  
“data”: [  
{  
“hostGroup”: {  
“hostGroupId”: “CL7-A,15”,  
“hostGroupName”: “host12”,  
“hostGroupNumber”: 15,  
“hostMode”: “VMWARE\_EX”,  
“hostModeOptions”: [  
54,  
63  
],  
“portId”: “CL7-A”,  
“storageDeviceId”: “886000999999”  
},  
“ldev”: {  
“attributes”: [  
“CVS”,  
“HDT”  
],  
“blockCapacity”: 2147483648,  
“byteFormatCapacity”: “1.00 T”,  
“clprId”: 0,  
“dataReductionMode”: “disabled”,  
“dataReductionStatus”: “DISABLED”,  
“emulationType”: “OPEN-V-CVS”,  
“isDefined”: true,  
“isFullAllocationEnabled”: false,  
“isRelocationEnabled”: true,  
“label”: “BackupSFTP”,  
“ldevId”: 82,  
“mpBladeId”: 0,  
“numOfUsedBlock”: 3096576,  
“poolId”: 0,  
“status”: “NML”,  
“storageDeviceId”: “886000999999”,  
“tierLevel”: “all”,  
“tierLevelForNewPageAllocation”: “M”,  
“usedCapacityPerTierLevel1”: 1512,  
“usedCapacityPerTierLevel2”: 0  
},  
“lun”: {  
“lun”: 5,  
“lunId”: “CL7-A,15,5”,  
“storageDeviceId”: “886000428027”  
}  
},  
{  
“hostGroup”: {  
“hostGroupId”: “CL7-B,15”,  
“hostGroupName”: “host12”,  
“hostGroupNumber”: 15,  
“hostMode”: “VMWARE\_EX”,  
“hostModeOptions”: [  
54,  
63  
],  
“portId”: “CL7-B”,  
“storageDeviceId”: “886000999999”  
},  
“ldev”: {  
“attributes”: [  
“CVS”,  
“HDT”  
],  
“blockCapacity”: 2147483648,  
“byteFormatCapacity”: “1.00 T”,  
“clprId”: 0,  
“dataReductionMode”: “disabled”,  
“dataReductionStatus”: “DISABLED”,  
“emulationType”: “OPEN-V-CVS”,  
“isDefined”: true,  
“isFullAllocationEnabled”: false,  
“isRelocationEnabled”: true,  
“label”: “BackupSFTP”,  
“ldevId”: 82,  
“mpBladeId”: 0,  
“numOfUsedBlock”: 3096576,  
“poolId”: 0,  
“status”: “NML”,  
“storageDeviceId”: “886000999999”,  
“tierLevel”: “all”,  
“tierLevelForNewPageAllocation”: “M”,  
“usedCapacityPerTierLevel1”: 1512,  
“usedCapacityPerTierLevel2”: 0  
},  
“lun”: {  
“lun”: 5,  
“lunId”: “CL7-B,15,5”,  
“storageDeviceId”: “886000428027”  
}  
},

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 23, 2019, 10:25pm UTC](https://forum.ansible.com/t/loop-through-a-json-object/31115/2 "2019-08-23T22:25:30Z")

</div>

It depends on how you are going to use it, you can get the value like so

&nbsp;&nbsp;&nbsp;{{ json\_out.json.data | map(attribute='hostGroup.hostGroupName') | list }}

or loop around it with Jinja

&nbsp;&nbsp;&nbsp;{% for i in json\_out.json.data %}  
&nbsp;&nbsp;&nbsp;{{ i.hostGroup.hostGroupName }}  
&nbsp;&nbsp;&nbsp;{{ i.lun.lun }}  
&nbsp;&nbsp;&nbsp;{{ i.lun.lunId }}  
&nbsp;&nbsp;&nbsp;{% endfor %}

---

<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:** [August 24, 2019, 12:17am UTC](https://forum.ansible.com/t/loop-through-a-json-object/31115/3 "2019-08-24T00:17:37Z")

</div>

It’s going to be used as input for a uri request. I will want to loop through each hostgroup and repeat until done with the list.

I have not looked yet but I will also want to filter the list by the name.

---

<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:** [August 24, 2019, 5:11am UTC](https://forum.ansible.com/t/loop-through-a-json-object/31115/4 "2019-08-24T05:11:12Z")

</div>

I got it to work with this:

`

- name : print all hostgroups  
debug:  
msg: “{{item.hostGroup.hostGroupName}},{{item.hostGroup.hostGroupId}}”  
loop: “{{sessions.json.data}}”  
when: item.hostGroup.hostGroupName | regex\_search(‘._am1._’)(.\*.9$)

`

`

`

`

I cant get the regex to work right. I am looking to match am1 anywhere in the string, but end with specific numbers such as 1-8 or 9-16. Any help there would be appreciated.

My next step will be to figure out how to do a loop and do something with that data. such as execute a uri block that will use the item.#### as inputs to the body of the uri call. I have not had much luck locating what i think would do that, if someone has an example that would be great.

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 24, 2019, 7:42am UTC](https://forum.ansible.com/t/loop-through-a-json-object/31115/5 "2019-08-24T07:42:55Z")

</div>

> I got it to work with this:
> 
> &nbsp;&nbsp;- name : print all hostgroups  
> &nbsp;&nbsp;&nbsp;&nbsp;debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{item.hostGroup.hostGroupName}},{{item.hostGroup.hostGroupId}}"  
> &nbsp;&nbsp;&nbsp;&nbsp;loop: "{{sessions.json.data}}"  
> &nbsp;&nbsp;&nbsp;&nbsp;when: item.hostGroup.hostGroupName | regex\_search('.\*am1.\*')(.\*\.9$)
> 
> I cant get the regex to work right. I am looking to match am1 anywhere  
> in the string, but end with specific numbers such as 1-8 or 9-16. Any  
> help there would be appreciated.

You can eliminate when by using Jinja filter, i would recommend reading about all of them[1]

&nbsp;&nbsp;&nbsp;- name : print all hostgroups  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{item.hostGroup.hostGroupName}},{{item.hostGroup.hostGroupId}}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ sessions.json.data | selectattr('hostGroup.hostGroupName', 'search', 'am1.\*9$') | list }}"

selectattr is a Jinja filter and search is a Ansible regex contains filter.

> My next step will be to figure out how to do a loop and do something with  
> that data. such as execute a uri block that will use the item.#### as  
> inputs to the body of the uri call. I have not had much luck locating  
> what i think would do that, if someone has an example that would be great.

Wouldn't that just be to use the uri module with the loop above and just {{ item.xxxx }} where every you need.

[1] [https://jinja.palletsprojects.com/en/2.10.x/templates/#list-of-builtin-filters](https://jinja.palletsprojects.com/en/2.10.x/templates/#list-of-builtin-filters)

---

<div class="post-metadata">

**Author:** ![CaptainBrewing](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@CaptainBrewing](https://forum.ansible.com/u/CaptainBrewing)\
**Post date:** [August 24, 2019, 3:41pm UTC](https://forum.ansible.com/t/loop-through-a-json-object/31115/6 "2019-08-24T15:41:12Z")

</div>

Kai I really appreciate your consistent and persistent responses on this board. I am learning a lot from you!

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 24, 2019, 4:34pm UTC](https://forum.ansible.com/t/loop-through-a-json-object/31115/7 "2019-08-24T16:34:11Z")

</div>

Thank you for the kind words, much appreciated.

---

<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:** [August 26, 2019, 5:42pm UTC](https://forum.ansible.com/t/loop-through-a-json-object/31115/8 "2019-08-26T17:42:47Z")

</div>

Thank you, that has helped alot.

I was looking at the max option:

`

- name: debug - show max lun number  
debug:  
msg: “{{item.lun.lun}}”  
loop: “{{luns.json.data}}”  
when: “{{item.lun.lun | max}}”

`

But it returns the following:

TASK [debug - show max lun number] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [127.0.0.1]: FAILED! =\> {“msg”: “The conditional check ‘{{item.lun.lun | max}}’ failed. The error was: Unexpected templating type error occurred on ({{item.lun.lun | max}}): ‘int’ object is not iterable\n\nThe error appears to be in ‘/home/nbritton/ansible/gts-core-storage-operations/hds/get\_luninfo.yml’: line 65, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: debug - show max lun number\n ^ here\n”}

The value i am after is the last one in the output here:

TASK [debug new vars] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [127.0.0.1] =\> {  
“msg”: “am1vmhost12,CL8-B,15,83,SFTP,6”  
}

---

<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:** [August 27, 2019, 1:12pm UTC](https://forum.ansible.com/t/loop-through-a-json-object/31115/9 "2019-08-27T13:12:39Z")

</div>

I got this figured out. I needed to add a step before, to create a list with all the values , then find the max of it.

---

<div class="post-metadata">

**Author:** ![Boa\_Ah](https://avatars.discourse-cdn.com/v4/letter/b/8baadc/32.png) [@Boa\_Ah](https://forum.ansible.com/u/Boa_Ah)\
**Post date:** [October 22, 2019, 10:35pm UTC](https://forum.ansible.com/t/loop-through-a-json-object/31115/10 "2019-10-22T22:35:35Z")

</div>

Hi  
Can you please post your whole task solution coz am having a similar need. My post is here: I need to pull all hostname a and ssh to them  
[https://groups.google.com/forum/m/#!topic/ansible-project/BInJVHkXvjo](https://groups.google.com/forum/m/#!topic/ansible-project/BInJVHkXvjo)
