Cannot filter with from_json

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

`
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

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

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

`
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?

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

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 :wink:

Regards, J

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 :slight_smile:

Ok, yes the practical way is the best but the harder… :wink: