# accessing json data

**URL:** <https://forum.ansible.com/t/accessing-json-data/14535>\
**Category:** Ansible Project\
**Created:** [February 10, 2014, 7:02pm UTC](https://forum.ansible.com/t/accessing-json-data/14535 "2014-02-10T19:02:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brent\_Langston1](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@Brent\_Langston1](https://forum.ansible.com/u/Brent_Langston1)\
**Post date:** [February 10, 2014, 7:02pm UTC](https://forum.ansible.com/t/accessing-json-data/14535/1 "2014-02-10T19:02:10Z")

</div>

I’m sure this is probably something I’m doing wrong, but I can’t seem to figure out the right thing, so hopefully someone else can.

This is the value of the registered variable “jenkinsStatus”: [http://hastebin.com/cacusiwiki.tex](http://hastebin.com/cacusiwiki.tex)

Given the following playbook:

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [February 10, 2014, 7:08pm UTC](https://forum.ansible.com/t/accessing-json-data/14535/2 "2014-02-10T19:08:24Z")

</div>

It seems like you need to index your array like so:

jenkinsStatus.json.changeSet.items[0].comment

Since the “.0.” would be looking for a hash member named “0” versus an array index.

---

<div class="post-metadata">

**Author:** ![Brent\_Langston1](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@Brent\_Langston1](https://forum.ansible.com/u/Brent_Langston1)\
**Post date:** [February 10, 2014, 7:14pm UTC](https://forum.ansible.com/t/accessing-json-data/14535/3 "2014-02-10T19:14:17Z")

</div>

I was wondering about that. I _did_ try that earlier (with so many other combinations as well.) This is what I have now:

debug: var=jenkinsStatus.json.changeSet.items[0].comment

result:

“jenkinsStatus.json.changeSet.items[0].comment”: “{{ jenkinsStatus.json.changeSet.items[0].comment }}”

I can’t figure out what’s wrong with that statement, though.

---

<div class="post-metadata">

**Author:** ![Brent\_Langston1](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@Brent\_Langston1](https://forum.ansible.com/u/Brent_Langston1)\
**Post date:** [February 10, 2014, 7:28pm UTC](https://forum.ansible.com/t/accessing-json-data/14535/4 "2014-02-10T19:28:42Z")

</div>

So I downloaded the json to a file, and tried to access that value using ‘jq’:

cat document.json | jq .json.changeSet.items[0].comment

that works fine, so I think you’re right about the syntax. Is it possible that Ansible can’t access objects nested that deep?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [February 10, 2014, 7:29pm UTC](https://forum.ansible.com/t/accessing-json-data/14535/5 "2014-02-10T19:29:40Z")

</div>

Nope.

Save it in a sample Python test.py file and get the right python syntax to survive a print statement, and then you’ll be on the right track.

---

<div class="post-metadata">

**Author:** ![Brent\_Langston1](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@Brent\_Langston1](https://forum.ansible.com/u/Brent_Langston1)\
**Post date:** [February 10, 2014, 7:53pm UTC](https://forum.ansible.com/t/accessing-json-data/14535/6 "2014-02-10T19:53:27Z")

</div>

This appears to work:

debug: var="{{jenkinsStatus.json.changeSet[“items”][0].comment}}”

My theory is that when the parser reached “items” in the json output, it called the python method “items” and received it’s output as a list.

In this case, it was necessary to surround the variable with “{{ }}”, and expressly reference [“items”], to get it to interpret correctly.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [February 10, 2014, 8:45pm UTC](https://forum.ansible.com/t/accessing-json-data/14535/7 "2014-02-10T20:45:40Z")

</div>

This could be true, actually. It wouldn’t call the method without parens but it would have errored out.

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [September 10, 2015, 9:07am UTC](https://forum.ansible.com/t/accessing-json-data/14535/8 "2015-09-10T09:07:48Z")

</div>

I hit this yesterday too.

Sorry to resurrect very old thread but thought I would add that in the end I wound up with the following syntax (to retrieve a list of conmmit messages from a jenkins build). Note that I wound up with single quotes around items:

- name: collect the commit messages  
set\_fact:  
commit\_messages: “{{ build\_info.json.changeSet[‘items’]|map(attribute=‘msg’)|list }}”
