Phil Di
Phil Di

Reputation: 21

extract several values from json using json query ansible

I m having a file like:

{
        "account": "/system1/test", 
        "description": "", 
        "desired_state": "active", 
        "id": "f957e20c-0033-4523-9020-e31304401149",
        "status": "active", 
        "tags": [], 
        "time_audited": "2018-01-14T23:12:22Z", 
        "time_created": "2018-01-14T22:34:53Z", 
        "time_updated": null
}

When I try to get a value out as:

with_items: "{{orch_details.content|from_json|json_query('status')}}"

Everything is fine.

I want to extract several other values but I can't combine them to the json_query like:

with_items: "{{orch_details.content|from_json|json_query('status, account, description')}}"

What is wrong with my syntax?

Upvotes: 2

Views: 6261

Answers (1)

Andy Shinn
Andy Shinn

Reputation: 28543

I think the comment is trying to point out that json_query uses JMESPath for the filters. You can read up a bit on it at http://jmespath.org/tutorial.html.

In your examples, you could do something like the following to get an array of the values:

with_items: "{{orch_details.content|from_json|json_query('[status, account, description]')}}"

Which would return:

[
  "active",
  "/system1/test",
  ""
]

Or if you want a new hash maybe:

with_items: "{{orch_details.content|from_json|json_query('{"status": status, "account": account, "desc": description]')}}"

Which would return:

{
  "status": "active",
  "account": "/system1/test",
  "desc": ""
}

Upvotes: 4

Related Questions