# Cannot filter with from\_json

**URL:** <https://forum.ansible.com/t/cannot-filter-with-from-json/32261>\
**Category:** Ansible Project\
**Created:** [February 18, 2020, 12:39pm UTC](https://forum.ansible.com/t/cannot-filter-with-from-json/32261 "2020-02-18T12:39:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hieronymus](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hieronymus/32/1624_2.png) [@hieronymus](https://forum.ansible.com/u/hieronymus)\
**Post date:** [February 18, 2020, 12:39pm UTC](https://forum.ansible.com/t/cannot-filter-with-from-json/32261/1 "2020-02-18T12:39:27Z")

</div>

Hi Team,

I’m encountered some issue with set\_facts from variable with json and for this reason I’m tried to understand this. To reproduce this case I’m using the example from here : [playbooks filters](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#filters-for-formatting-data)

`  
tasks

- shell: cat /some/path/to/file.json  
register: result

- set\_fact:  
myvar: “{{ result.stdout | from\_json }}”  
`

I’ve created one example.fson file :

`

# cat files/example.json

json\_example: |  
{  
“example\_simple”: {  
“name”: “simple”,  
“foo”: “value”,  
“item”: “this”  
},  
}  
`

Here’s the task file :

`

# cat tasks/main.yml

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 18, 2020, 3:49pm UTC](https://forum.ansible.com/t/cannot-filter-with-from-json/32261/2 "2020-02-18T15:49:04Z")

</div>

This is not correct json syntax.  
It looks like yaml json\_example multiline with an incorrectly syntax json in  
it.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 18, 2020, 3:51pm UTC](https://forum.ansible.com/t/cannot-filter-with-from-json/32261/3 "2020-02-18T15:51:03Z")

</div>

> Hi Team,
> 
> I’m encountered some issue with set\_facts from variable with json and for this reason I’m tried to understand this. To reproduce this case I’m using the example from here : [playbooks filters](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#filters-for-formatting-data)
> 
> `  
> tasks
> 
> - shell: cat /some/path/to/file.json  
> register: result
> 
> - set\_fact:  
> myvar: “{{ result.stdout | from\_json }}”  
> `

This here below looks like your example.json file actually contains yaml which in turn contains json?

---

<div class="post-metadata">

**Author:** ![Angel\_Rengifo\_Cancin](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@Angel\_Rengifo\_Cancin](https://forum.ansible.com/u/Angel_Rengifo_Cancin)\
**Post date:** [February 19, 2020, 12:19am UTC](https://forum.ansible.com/t/cannot-filter-with-from-json/32261/4 "2020-02-19T00:19:52Z")

</div>

Hello:

> I’ve created one example.fson file :
> 
> `
> 
> # cat files/example.json
> 
> json\_example: |  
> {  
> “example\_simple”: {  
> “name”: “simple”,  
> “foo”: “value”,  
> “item”: “this”  
> },  
> }  
> `

Your JSON has an incorrect syntax. This is how should look like:

{  
“json\_example”: {  
“example\_simple”: {  
“name”: “simple”,  
“foo”: “value”,  
“item”: “this”  
}  
}  
}

This way, your playbook just runs fine:

- shell: cat example.json  
register: result

- set\_fact:  
myvar: “{{ result.stdout | from\_json }}”

- debug: var=myvar

which produces this output:

TASK [shell] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [localhost]

TASK [set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“myvar”: {  
“json\_example”: {  
“example\_simple”: {  
“foo”: “value”,  
“item”: “this”,  
“name”: “simple”  
}  
}  
}  
}

You can use this to parse your JSON files:

$ python -m json.tool \< example.json

As Dick Visser said, it seems you’re mixed some kind of YAML+JSON content which invalidates it at all

---

<div class="post-metadata">

**Author:** ![hieronymus](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hieronymus/32/1624_2.png) [@hieronymus](https://forum.ansible.com/u/hieronymus)\
**Post date:** [February 19, 2020, 9:37am UTC](https://forum.ansible.com/t/cannot-filter-with-from-json/32261/5 "2020-02-19T09:37:57Z")

</div>

Thank you all for your help and support.

Yes, now it’s work.

@Angel: I’m really newbie in this kind of scripting and I do mix it up a little bit with these format.  
If you have a clever tutorial, I want it 😉

Regards, J

---

<div class="post-metadata">

**Author:** ![Angel\_Rengifo\_Cancin](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@Angel\_Rengifo\_Cancin](https://forum.ansible.com/u/Angel_Rengifo_Cancin)\
**Post date:** [February 19, 2020, 12:14pm UTC](https://forum.ansible.com/t/cannot-filter-with-from-json/32261/6 "2020-02-19T12:14:49Z")

</div>

Sorry, I don’t have a tutorial for that. I’m planning to do one, in a near future.

I also suffered a little bit with JSON and its structures, because I didn’t have clear concepts of objects and arrays. Just practice made the difference for me 🙂

---

<div class="post-metadata">

**Author:** ![hieronymus](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hieronymus/32/1624_2.png) [@hieronymus](https://forum.ansible.com/u/hieronymus)\
**Post date:** [February 19, 2020, 12:41pm UTC](https://forum.ansible.com/t/cannot-filter-with-from-json/32261/7 "2020-02-19T12:41:52Z")

</div>

Ok, yes the practical way is the best but the harder… 😉
