# Accessing a sub-variable from a JSON output

**URL:** <https://forum.ansible.com/t/accessing-a-sub-variable-from-a-json-output/29282>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [December 18, 2018, 11:15am UTC](https://forum.ansible.com/t/accessing-a-sub-variable-from-a-json-output/29282 "2018-12-18T11:15:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JS11](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@JS11](https://forum.ansible.com/u/JS11)\
**Post date:** [December 18, 2018, 11:15am UTC](https://forum.ansible.com/t/accessing-a-sub-variable-from-a-json-output/29282/1 "2018-12-18T11:15:18Z")

</div>

Hello

I have a variable that contains output from a json file.

`

- name: set fact  
set\_fact:  
test123: “{{ jsonVar | json\_query(name\_query) }}”  
vars:  
name\_query: “.{name: name, ssh\_url\_to\_repo: ssh\_url\_to\_repo namespace\_path: namespace.full\_path }”

`

This is the output the command above produces:

`

ok: [host1] =\> {  
“ansible\_facts”: {  
“test123”: [  
{  
“name”: “appscomb”,  
“namespace\_path”: “aws/docker”,  
“ssh\_url\_to\_repo”: “ssh://git@gitlab.example.local:4444/aws/docker/appscomb.git”  
},  
{  
“name”: “appscomb”,  
“namespace\_path”: “software-repositories”,  
“ssh\_url\_to\_repo”: “ssh://git@gitlab.example.local:4444/software-repositories/appscomb.git”  
},  
{  
“name”: “java”,  
“namespace\_path”: “software-repositories”,  
“ssh\_url\_to\_repo”: “ssh://git@gitlab.example.local:4444/software-repositories/java.git”  
},  
{  
“name”: “DMI”,  
“namespace\_path”: “dream-it/docker”,  
“ssh\_url\_to\_repo”: “ssh://git@gitlab.example.local:4444/dream-it/docker/dmi.git”  
},  
{  
“name”: “gitlab”,  
“namespace\_path”: “aws/docker”,  
“ssh\_url\_to\_repo”: “ssh://git@gitlab.example.local:4444/aws/docker/gitlab.git”  
},  
{  
“name”: “os-management”,  
“namespace\_path”: “aws/ansible”,  
“ssh\_url\_to\_repo”: “ssh://git@gitlab.example.local:4444/aws/ansible/os-management.git”  
},  
{  
“name”: “appscomb”,  
“namespace\_path”: “aws/ansible”,  
“ssh\_url\_to\_repo”: “ssh://git@gitlab.example.local:4444/aws/ansible/appscomb.git”  
},  
{  
“name”: “xen-management”,  
“namespace\_path”: “aws/ansible”,  
“ssh\_url\_to\_repo”: “ssh://git@gitlab.example.local:4444/aws/ansible/xen-management.git”  
},  
{  
“name”: “aws-docker”,  
“namespace\_path”: “aws/docker”,  
“ssh\_url\_to\_repo”: “ssh://git@gitlab.example.local:4444/aws/docker/aws-docker.git”  
},  
{  
“name”: “dbarchery”,  
“namespace\_path”: “dream-it/ansible”,  
“ssh\_url\_to\_repo”: “ssh://git@gitlab.example.local:4444/dream-it/ansible/dbarchery.git”  
},  
{  
“name”: “dbarchery”,  
“namespace\_path”: “dream-it/docker”,  
“ssh\_url\_to\_repo”: “ssh://git@gitlab.example.local:4444/dream-it/docker/dbarchery.git”  
}  
]  
},  
“changed”: false  
}

`

How do I access for example, the **name** or **namespace\_path** or **ssh\_url\_to\_repo** of any or all of the above?

I have tried this:

`

- name: debug  
debug:

msg: “{{ test123.name }}”

# with\_items: “{{ test123 }}”

`

and this:

`

- name: debug  
debug:

msg: “{{ test123[‘name’] }}”

# with\_items: “{{ test123 }}”

`

But they both seem to be failing with the following error:

`

fatal: [host1]: FAILED! =\> {  
“msg”: “The task includes an option with an undefined variable. The error was: ‘list object’ has no attribute ‘name’\n\nThe error appears to have been in ‘/home/gitlab/roles/gitlab-backup\_repos/tasks/main.yml’: line 25, column 3, 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\n ^ here\n”

`

Many Thanks in Advance!  
JS

---

<div class="post-metadata">

**Author:** ![Eric\_Brewer](https://avatars.discourse-cdn.com/v4/letter/e/a5b964/32.png) [@Eric\_Brewer](https://forum.ansible.com/u/Eric_Brewer)\
**Post date:** [December 18, 2018, 4:39pm UTC](https://forum.ansible.com/t/accessing-a-sub-variable-from-a-json-output/29282/2 "2018-12-18T16:39:09Z")

</div>

I think you need to declare ansible\_facts before your variable, and then you need to iterate through the array so try this:

`

`

- name: debug  
debug:  
msg: “{{ item[‘name’] }}”  
with\_items: “{{ ansible\_facts[‘test123’] }}”  
`

`

And if you don’t want to iterate through all of them, you need to call the specific array location since you have placed them in a list:

`ansible_facts['test123'][0]['name']`

That should get you the first one.

---

<div class="post-metadata">

**Author:** ![JS11](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@JS11](https://forum.ansible.com/u/JS11)\
**Post date:** [December 19, 2018, 11:39am UTC](https://forum.ansible.com/t/accessing-a-sub-variable-from-a-json-output/29282/3 "2018-12-19T11:39:58Z")

</div>

Hi Eric

Brilliant! Thank you very much! - That put me down the right path.

Once I understood your syntax, I managed to get all of these to work:

`

- name: debug 0  
debug:  
var: test123

- name: debug 1  
debug:  
var: test123[0].namespace\_path

- name: debug 2  
debug:  
msg: The value is “{{ test123[0].namespace\_path }}”

- name: debug 3  
debug:  
var: item[‘namespace\_path’]  
with\_items: “{{ test123 }}”

- name: debug 4  
debug:  
msg: The value is “{{ item[‘name’] }}”  
with\_items: “{{ test123 }}”

`

Once again, your assistance has been very much appreciated!

Regards

Jinal
