I have a JSON response from the server that looks like this:
{
"rules": [
{
"id": "1234",
"disabled": false,
"condition": [
"or",
[
"contains",
[
"path",
"payload",
"custom_details",
"resolved"
],
"Platform"
],
[
"contains",
[
"path",
"payload",
"custom_details",
"firing"
],
"Platform"
]
],
"catch_all": false,
"advanced_condition": [],
"actions": [
[
"route",
"XYZ123"
],
[
"extract",
"^\\[.*\\] *([^ ]*)",
[
"path",
"payload",
"summary"
],
"description"
]
]
},
{
"id": "9876",
"disabled": false,
"condition": [
"or",
[
"contains",
[
"path",
"payload",
"custom_details",
"resolved"
],
"Sidewalk"
],
[
"contains",
[
"path",
"payload",
"custom_details",
"firing"
],
"Sidewalk"
]
],
"catch_all": false,
"advanced_condition": [],
"actions": [
[
"route",
"QWERT1"
],
[
"extract",
"^\\[.*\\] *([^ ]*)",
[
"path",
"payload",
"summary"
],
"description"
]
]
}
]
I want to loop over this and match the string Sidewalk
. If there is a match, then get the value of id
of the matched.
I tried this
---
- name: API call - GET Event Rule ID
uri:
url: "{{ api_event_rules }}"
method: GET
status_code: 200
headers:
Content-Type: "application/json"
Accept: "application/vnd.ap+json;version=2"
Authorization: "Token token={{ api_token }}"
register: json_response
I tried this JMESPath
rules[*].condition[*][2]
But this gives out something like this:
[
[
"Platform",
"Platform"
],
[
"Sidewalk",
"Sidewalk"
],
I'm not able to find my way past this, very new to ansible. Any help is much appreciated.
Q: "Match the string Sidewalk. If there is a match, then get the value of id of the matched."
A: It's not clear which 'Sidewalk' we shall look for. Let's take first one. The task below creates a dictionary of id and the corresponding condition
- set_fact:
ids: "{{ rules|
json_query('[*].{key: condition[1][2], value: id}')|
items2dict
}}"
- debug:
var: ids['Sidewalk']
The debug gives
"ids['Sidewalk']": "9876"
If this shall be a conditional task the debug below
- debug:
msg: "Sidewalk found with id: {{ ids['Sidewalk'] }}"
when: "'Sidewalk' in ids.keys()"
gives
"msg": "Sidewalk found with id: 9876"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.