# JMESPath-like expression to traverse Ansible variables

**URL:** <https://forum.ansible.com/t/jmespath-like-expression-to-traverse-ansible-variables/26480>\
**Category:** Ansible Project\
**Created:** [January 15, 2018, 4:13pm UTC](https://forum.ansible.com/t/jmespath-like-expression-to-traverse-ansible-variables/26480 "2018-01-15T16:13:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manfredi\_Giordano](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@Manfredi\_Giordano](https://forum.ansible.com/u/Manfredi_Giordano)\
**Post date:** [January 15, 2018, 4:13pm UTC](https://forum.ansible.com/t/jmespath-like-expression-to-traverse-ansible-variables/26480/1 "2018-01-15T16:13:42Z")

</div>

Hello all,

I’m struggling to find a way to use _[] inside a query to traverse an Ansible variable.  
The content of the variable comes from a JSON file. The error message I get is "\_template error while templating string: expected name or number. String: {{ result.root._.component }}\_".

Let’s say the variable (result) contains the following JSON content:

`  
{  
“root”: {  
“DEV”: [  
{  
“component”: {  
“artifactId”: “component-1”,  
“version”: “1.0.0”  
}  
}  
],  
“STG”: [  
{  
“component”: {  
“artifactId”: “component-2”,  
“version”: “1.0.1”  
},  
“runtimeConf”: {  
“artifactId”: “runtime-configuration”,  
“version”: “1.0.1”  
}  
}  
]  
}  
}

`

My Ansible code tries to extract the values of all the _“component”_ attributes as a list, regardless of the external attributes where the _“component”_ attributes are defined (in the example, _“DEV”_ and “STG”). These external attributes are not known beforehand, hence the need for that \* in the query.

`  
tasks:

- set\_fact:  
component\_list: “{{ result.root.\*.component }}”

`

The output I expect is:

`

```auto
[
  {
    "artifactId": "component-1"
    "version": "1.0.0"
  },
  {
    "artifactId": "component-2",
    "version": "1.0.1"
  }
]

```

`  
That is, a list of all the objects whose attribute was _“component”_ in the input JSON.

I’ve tried to execute the same query on the [JMESPath website](http://jmespath.org/) with the same input and the result is the one I expect. Unfortunately, this is not the case while executing the task with Ansible.

Am I doing something wrong here? Thanks in advance for your help.

---

<div class="post-metadata">

**Author:** ![Manfredi\_Giordano](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@Manfredi\_Giordano](https://forum.ansible.com/u/Manfredi_Giordano)\
**Post date:** [January 15, 2018, 4:15pm UTC](https://forum.ansible.com/t/jmespath-like-expression-to-traverse-ansible-variables/26480/2 "2018-01-15T16:15:56Z")

</div>

Sorry I forgot, I am using Ansible 2.4.2.0 with Python 2.7.12.

---

<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:** [January 16, 2018, 8:12am UTC](https://forum.ansible.com/t/jmespath-like-expression-to-traverse-ansible-variables/26480/3 "2018-01-16T08:12:34Z")

</div>

You need to use the json\_query filter (and install jmespath module on your ansible controller machine) if you want to use JMESPath filtering.

See

[http://docs.ansible.com/ansible/latest/playbooks\_filters.html#json-query-filter](http://docs.ansible.com/ansible/latest/playbooks_filters.html#json-query-filter)

There’s an example there that looks fairly similar to what you are trying to do.

Hope this helps,

Jon

---

<div class="post-metadata">

**Author:** ![Manfredi\_Giordano](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@Manfredi\_Giordano](https://forum.ansible.com/u/Manfredi_Giordano)\
**Post date:** [January 16, 2018, 10:11am UTC](https://forum.ansible.com/t/jmespath-like-expression-to-traverse-ansible-variables/26480/4 "2018-01-16T10:11:33Z")

</div>

Hello Jon,

Thanks for your answer.  
I hoped to be able to do it without `json_query` (and thus without `jmespath`), but I don’t think it’s possible.
