# Problem looping over JSON data returned from URI call

**URL:** <https://forum.ansible.com/t/problem-looping-over-json-data-returned-from-uri-call/19785>\
**Category:** Ansible Project\
**Created:** [November 17, 2015, 4:30pm UTC](https://forum.ansible.com/t/problem-looping-over-json-data-returned-from-uri-call/19785 "2015-11-17T16:30:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andris\_Bjornson](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@Andris\_Bjornson](https://forum.ansible.com/u/Andris_Bjornson)\
**Post date:** [November 17, 2015, 4:30pm UTC](https://forum.ansible.com/t/problem-looping-over-json-data-returned-from-uri-call/19785/1 "2015-11-17T16:30:25Z")

</div>

Hello - I have a question about looping over json data I’ve received from calling a REST api using the URI module. I’ve been tearing my hair out over the last several days trying to sort this out…and have googled extensively - but haven’t gotten anywhere. I think it has to do with the difference between a dict, and array, and a list…but i’m new-ish to ansible and not understanding what i’m doing wrong. Any help greatly appreciated.

Here is the data returned and registered by the URI module call. Lets say I registered it in a variable called “registered\_variable” [https://gist.github.com/andris/77dcdfd17eef0cdd3563](https://gist.github.com/andris/77dcdfd17eef0cdd3563)

I can access individual items from the array by doing this: registered\_variable.json.data[1]

I can access attributes of individual items from the array by doing this: registered\_variable.json.data[1].name

But whenever I try to use with\_dict or with\_items to loop over registered\_variable.json.data…I just get an “item is undefined error” (for example)

- debug:  
msg: “{{item.name}} is the name of the device”  
with\_dict: “{{registered\_variable.json.data}}”

I also tried first storing the object in a variable first:

- set\_fact:  
devices: “{{ registered\_variable.json.data }}”
- debug:  
msg: “{{item.name}} is the device name”  
with\_dict: devices

I am running on the latest dev from checkout (2.0.0)

Any suggestions?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Brian\_Green](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@Brian\_Green](https://forum.ansible.com/u/Brian_Green)\
**Post date:** [November 18, 2015, 11:32pm UTC](https://forum.ansible.com/t/problem-looping-over-json-data-returned-from-uri-call/19785/2 "2015-11-18T23:32:24Z")

</div>

Try using from\_json filter

[http://docs.ansible.com/ansible/playbooks\_filters.html#filters-for-formatting-data](http://docs.ansible.com/ansible/playbooks_filters.html#filters-for-formatting-data)

[https://gist.github.com/lxhunter/45fb119c0128600158d8](https://gist.github.com/lxhunter/45fb119c0128600158d8)

---

<div class="post-metadata">

**Author:** ![Andris\_Bjornson](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@Andris\_Bjornson](https://forum.ansible.com/u/Andris_Bjornson)\
**Post date:** [November 19, 2015, 2:40pm UTC](https://forum.ansible.com/t/problem-looping-over-json-data-returned-from-uri-call/19785/3 "2015-11-19T14:40:28Z")

</div>

Hi Brian - thanks for the reply.

I had found from\_json…but then I realized that the uri module is actually putting stuff into an attribute of registered\_variable called registered\_variable.json.

I did solve my problem, though…it was a bonehead YAML syntax issue with the indents of my with\_items statement.

I had:

- set\_fact:  
devices: “{{ registered\_variable.json.data }}”
- debug:  
msg: “{{[item.name](http://item.name/)}} is the device name”  
with\_items: devices

When I should have had:

- set\_fact:  
devices: “{{ registered\_variable.json.data }}”
- debug:  
msg: “{{[item.name](http://item.name/)}} is the device name”  
with\_items: devices
