accessing json data

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

Given the following playbook:

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.

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.

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?

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.

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.

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

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 }}”