# Parse JSON output from uri module

**URL:** <https://forum.ansible.com/t/parse-json-output-from-uri-module/16789>\
**Category:** Ansible Project\
**Created:** [November 14, 2014, 4:27pm UTC](https://forum.ansible.com/t/parse-json-output-from-uri-module/16789 "2014-11-14T16:27:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Petr\_Sukharev](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@Petr\_Sukharev](https://forum.ansible.com/u/Petr_Sukharev)\
**Post date:** [November 14, 2014, 4:27pm UTC](https://forum.ansible.com/t/parse-json-output-from-uri-module/16789/1 "2014-11-14T16:27:33Z")

</div>

Hello! I have some strange problem with register option in role and json fomat. I try to communicate with my Monitoring System (Zabbix) via Zabbix API. A need to send some data, collect some data and make some work around results.

Here is output:

PLAY [all] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
\<10.30.42.104\> ESTABLISH CONNECTION FOR USER: root on PORT 22 TO 10.30.42.104  
\<10.30.42.104\> REMOTE\_MODULE setup  
\<10.30.42.104\> EXEC /bin/sh -c ‘mkdir -p $HOME/.ansible/tmp/ansible-tmp-1415981568.28-16153856594414 && echo $HOME/.ansible/tmp/ansible-tmp-1415981568.28-16153856594414’  
\<10.30.42.104\> PUT /tmp/tmpQ8RBGU TO /root/.ansible/tmp/ansible-tmp-1415981568.28-16153856594414/setup  
\<10.30.42.104\> EXEC /bin/sh -c ‘LC\_CTYPE=en\_US.UTF-8 LANG=en\_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1415981568.28-16153856594414/setup; rm -rf /root/.ansible/tmp/ansible-tmp-1415981568.28-16153856594414/ \>/dev/null 2\>&1’  
ok: [[test-machine.example.com](http://test-machine.example.com)]  
[test-machine.example.com](http://test-machine.example.com): importing /etc/ansible/vars/example-domain/example-domain  
Perform task: zabbix\_agent\_setup | get auth data for zabbix server (y/n/c): c

Perform task: zabbix\_agent\_setup | get auth data for zabbix server (y/n/c): \*\*\*  
 REMOTE\_MODULE uri url=http://“10.20.0.83”/api\_jsonrpc.php HEADER\_Content-Type=“application/json” method=POST body=“{"jsonrpc":"2.0", "method":"user.login", "params":{"user":"Admin","password":"SECUREPASSWORD"},"id":"1"}”  
 EXEC [‘/bin/sh’, ‘-c’, ‘mkdir -p $HOME/.ansible/tmp/ansible-tmp-1415981575.59-193170270205493 && echo $HOME/.ansible/tmp/ansible-tmp-1415981575.59-193170270205493’]  
 PUT /tmp/tmp2H04FO TO /root/.ansible/tmp/ansible-tmp-1415981575.59-193170270205493/uri  
 EXEC [‘/bin/sh’, ‘-c’, u’LC\_CTYPE=en\_US.UTF-8 LANG=en\_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1415981575.59-193170270205493/uri; rm -rf /root/.ansible/tmp/ansible-tmp-1415981575.59-193170270205493/ \>/dev/null 2\>&1’]  
ok: [[test-machine.example.com](http://test-machine.example.com)] =\> {“changed”: false, “connection”: “keep-alive”, “content\_type”: “application/json”, “date”: “Fri, 14 Nov 2014 16:12:55 GMT”, “json”: {“id”: “1”, “jsonrpc”: “2.0”, “result”: “4defb454bb5424ee06e8c7c6d1456eb6”}, “keep\_alive”: “timeout=1”, “redirected”: false, “server”: “nginx”, “status”: 200, “transfer\_encoding”: “chunked”, “x\_powered\_by”: “PHP/5.3.3”}

TASK: [zabbix\_agent\_setup | debug var={{zabbix\_auth\_code.json.result}}] \*\*\*\*\*\*\*  
\<10.30.42.104\> ESTABLISH CONNECTION FOR USER: root on PORT 22 TO 10.30.42.104  
ok: [[test-machine.example.com](http://test-machine.example.com)] =\> {  
“4defb454bb5424ee06e8c7c6d1456eb6”: “{{ 4defb454bb5424ee06e8c7c6d1456eb6 }}”  
}

TASK: [zabbix\_agent\_setup | check does host is present in zabbix] \*\*\*\*\*\*\*\*\*\*\*\*\*  
 REMOTE\_MODULE uri url=http://“10.20.0.83”/api\_jsonrpc.php HEADER\_Content-Type=“application/json” method=POST body=“{"jsonrpc":"2.0","method":"host.exists","params":{"nodeids":["1","2"],"host":"test-machine.example.com"},"auth":"4defb454bb5424ee06e8c7c6d1456eb6","id":2}”  
 EXEC [‘/bin/sh’, ‘-c’, ‘mkdir -p $HOME/.ansible/tmp/ansible-tmp-1415981578.99-29641969136951 && echo $HOME/.ansible/tmp/ansible-tmp-1415981578.99-29641969136951’]  
 PUT /tmp/tmpdXNLM0 TO /root/.ansible/tmp/ansible-tmp-1415981578.99-29641969136951/uri  
 EXEC [‘/bin/sh’, ‘-c’, u’LC\_CTYPE=en\_US.UTF-8 LANG=en\_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1415981578.99-29641969136951/uri; rm -rf /root/.ansible/tmp/ansible-tmp-1415981578.99-29641969136951/ \>/dev/null 2\>&1’]  
ok: [[test-machine.example.com](http://test-machine.example.com)] =\> {“changed”: false, “connection”: “keep-alive”, “content\_type”: “application/json”, “date”: “Fri, 14 Nov 2014 16:12:59 GMT”, “json”: {“id”: 2, “jsonrpc”: “2.0”, “result”: false}, “keep\_alive”: “timeout=1”, “redirected”: false, “server”: “nginx”, “status”: 200, “transfer\_encoding”: “chunked”, “x\_powered\_by”: “PHP/5.3.3”}

TASK: [zabbix\_agent\_setup | debug var={{zabbix\_host\_exist.json.result}}] \*\*\*\*\*\*  
\<10.30.42.104\> ESTABLISH CONNECTION FOR USER: root on PORT 22 TO 10.30.42.104  
ok: [[test-machine.example.com](http://test-machine.example.com)] =\> {  
“False”: “False”  
}

TASK: [zabbix\_agent\_setup | get discoved hosts group id from zabbix] \*\*\*\*\*\*\*\*\*\*  
 REMOTE\_MODULE uri url=http://“10.20.0.83”/api\_jsonrpc.php HEADER\_Content-Type=“application/json” method=POST body=“{"jsonrpc":"2.0","method":"hostgroup.getobjects","params":{"name":"Discovered hosts"},"auth":"4defb454bb5424ee06e8c7c6d1456eb6","id":3}”  
 EXEC [‘/bin/sh’, ‘-c’, ‘mkdir -p $HOME/.ansible/tmp/ansible-tmp-1415981582.72-146841785813622 && echo $HOME/.ansible/tmp/ansible-tmp-1415981582.72-146841785813622’]  
 PUT /tmp/tmpaiW2bl TO /root/.ansible/tmp/ansible-tmp-1415981582.72-146841785813622/uri  
 EXEC [‘/bin/sh’, ‘-c’, u’LC\_CTYPE=en\_US.UTF-8 LANG=en\_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1415981582.72-146841785813622/uri; rm -rf /root/.ansible/tmp/ansible-tmp-1415981582.72-146841785813622/ \>/dev/null 2\>&1’]  
ok: [[test-machine.example.com](http://test-machine.example.com)] =\> {“changed”: false, “connection”: “keep-alive”, “content\_type”: “application/json”, “date”: “Fri, 14 Nov 2014 16:13:02 GMT”, “json”: {“id”: 3, “jsonrpc”: “2.0”, “result”: [{“flags”: “0”, “groupid”: “100100000000005”, “internal”: “1”, “name”: “Discovered hosts”}]}, “keep\_alive”: “timeout=1”, “redirected”: false, “server”: “nginx”, “status”: 200, “transfer\_encoding”: “chunked”, “x\_powered\_by”: “PHP/5.3.3”}

TASK: [zabbix\_agent\_setup | debug var={{zabbix\_group\_id.json.result.groupid}}] \*\*\*  
\<10.30.42.104\> ESTABLISH CONNECTION FOR USER: root on PORT 22 TO 10.30.42.104  
fatal: [[test-machine.example.com](http://test-machine.example.com)] =\> One or more undefined variables: ‘list’ object has no attribute ‘groupid’

FATAL: all hosts have already failed – aborting

---

<div class="post-metadata">

**Author:** ![Romeo\_Theriault](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@Romeo\_Theriault](https://forum.ansible.com/u/Romeo_Theriault)\
**Post date:** [November 14, 2014, 7:52pm UTC](https://forum.ansible.com/t/parse-json-output-from-uri-module/16789/2 "2014-11-14T19:52:12Z")

</div>

It looks like zabbix is passing back an array containing the json data.

Try this instead: {{ zabbix\_group\_id.json.result[0].groupid }}

If that doesn’t work, what do you get when you do: {{ zabbix\_group\_id.json.result[0] }} or just {{ zabbix\_group\_id.json.result }}

---

<div class="post-metadata">

**Author:** ![Petr\_Sukharev](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@Petr\_Sukharev](https://forum.ansible.com/u/Petr_Sukharev)\
**Post date:** [November 14, 2014, 8:25pm UTC](https://forum.ansible.com/t/parse-json-output-from-uri-module/16789/3 "2014-11-14T20:25:25Z")

</div>

Working like charm, thanks!

i am little confused, my first and second steps get correct data, but now i need to use result[0]…

---

<div class="post-metadata">

**Author:** ![Toshio\_Kuratomi](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Toshio\_Kuratomi](https://forum.ansible.com/u/Toshio_Kuratomi)\
**Post date:** [November 16, 2014, 5:23pm UTC](https://forum.ansible.com/t/parse-json-output-from-uri-module/16789/4 "2014-11-16T17:23:57Z")

</div>

Each module is in charge of deciding what data structures to return  
and in what hierarchy so if you need the returned data from a call to  
a particular piece of module functionality you do have to check that  
the return value is formatted the way you expect. In this case I think  
that the first two zabbix calls only return a single value but the  
last function can return multiple values if there are multiple hosts  
or groupids. So the zabbix module author chose to return an array in  
the latter instance.

-Toshio

---

<div class="post-metadata">

**Author:** ![Michael\_Liu1](https://avatars.discourse-cdn.com/v4/letter/m/4491bb/32.png) [@Michael\_Liu1](https://forum.ansible.com/u/Michael_Liu1)\
**Post date:** [April 17, 2015, 2:26am UTC](https://forum.ansible.com/t/parse-json-output-from-uri-module/16789/5 "2015-04-17T02:26:45Z")

</div>

Thanks this was very helpful. Banged my head against the keyboard for about 2 hours before I found this page.
