Cannot filter with from_json

183 views
Skip to first unread message

Jerome Meyer

unread,
Feb 18, 2020, 7:39:27 AM2/18/20
to Ansible Project
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
---
- name: Read JSON file
  shell
: cat ../files/example.json
 
register: json
 
- name: Get simple value.
  set_fact
:
    simple_value
: "{{ (json.stdout | from_json).example_simple.name }}"


- name: Simple debug.
  debug
:
    msg
: "{{ simple_value }}"


And here the result:
fatal: [localhost]: FAILED! => {"msg": "the field 'args' has an invalid value ({u'simple_value': u'{{ (json.stdout | from_json) }}'}), and could not be converted to an dict.The error was: No JSON object could be decoded\n\nThe error appears to be in '/etc/ansible/roles/testrole/tasks/main.yml': line 7, 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: Get simple value.\n  ^ here\n"}

It tells me that the input cannot be converted to dict, But why and what is the problem? Could someone explain me this problem?
I cannot understand what is wrong.

Best regards, J






Kai Stian Olstad

unread,
Feb 18, 2020, 10:49:24 AM2/18/20
to ansible...@googlegroups.com
On Tue, Feb 18, 2020 at 04:39:27AM -0800, Jerome Meyer wrote:
> 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"
> },
> }

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


--
Kai Stian Olstad

Dick Visser

unread,
Feb 18, 2020, 10:51:35 AM2/18/20
to ansible...@googlegroups.com
On Tue, 18 Feb 2020 at 13:39, Jerome Meyer <jer.m...@gmail.com> wrote:
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?

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/76189198-429c-4025-98f8-92e7f69ff417%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Angel Rengifo Cancino

unread,
Feb 18, 2020, 7:20:18 PM2/18/20
to ansible...@googlegroups.com
Hello:

On Tue, Feb 18, 2020 at 7:39 AM Jerome Meyer <jer.m...@gmail.com> wrote:
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

Jerome Meyer

unread,
Feb 19, 2020, 4:37:57 AM2/19/20
to Ansible Project
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


On Wednesday, February 19, 2020 at 1:20:18 AM UTC+1, Angel Rengifo Cancino wrote:
Hello:

Angel Rengifo Cancino

unread,
Feb 19, 2020, 7:15:17 AM2/19/20
to ansible...@googlegroups.com
On Wed, Feb 19, 2020 at 4:38 AM Jerome Meyer <jer.m...@gmail.com> wrote:
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  ;)

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

Jerome Meyer

unread,
Feb 19, 2020, 7:41:52 AM2/19/20
to Ansible Project

On Wednesday, February 19, 2020 at 1:15:17 PM UTC+1, Angel Rengifo Cancino wrote:
Ok, yes the practical way is the best but the harder... ;)
Reply all
Reply to author
Forward
0 new messages