How to unquote a json structure

82 views
Skip to first unread message

jean-christophe manciot

unread,
Jul 28, 2022, 5:59:54 AM7/28/22
to Ansible Project
One shell command returns a json structure, but ansible converts it into a string in the registered variable.
In order to access some attributes, this string needs to be unquoted, but how?
There is a quote filter, but there is no unquote filter in ansible or jinja2.

For instance, let's suppose a shell module returns the following registered variable:
    return:
      changed: true
      cmd: |-
        cmd_returning_json
      delta: '0:00:00.002283'
      end: '2022-07-28 11:09:17.921175'
      failed: false
      msg: ''
      rc: 0
      start: '2022-07-28 11:09:17.918892'
      stderr: ''
      stderr_lines: []
      stdout: '{"class": "data", "id": "a61b96e7-427e-493a-993c-c3efc8a16aa1", "size": "500GB", "type": "ssd"}'

How do we convert return.stdout into:
{"class": "data", "id": "a61b96e7-427e-493a-993c-c3efc8a16aa1", "size": "500GB", "type": "ssd"}
so that we can access for instance: 

jean-christophe manciot

unread,
Jul 28, 2022, 6:10:00 AM7/28/22
to Ansible Project
It turns out there is no need for unquoting.
In my original issue, the returned structure was much more complex and included some strange new lines.
Removing all new lines with:
return.stdout |  regex_replace('\\n', '')
solves this issue.
Reply all
Reply to author
Forward
0 new messages