如何通过 json_query 从数组中提取对象?
How to extract object from array by json_query?
我从下面的变量中得到了 json。
[
{
"policyName1": {
"resourceType": "Name1Type",
"id": "uuid-0000-000-0-1",
"extraAttr1":
[
{
"network11":
{
"allowAction": "yes",
"ipAddresses":
[
"192.168.29.193",
"192.168.29.196"
],
"disabled": false,
"ip_protocol": "IPV4_IPV6"
}
}
]
}
},
{
"policyName2": {
"resourceType": "name2Type",
"id": "uuid-0000-000-0-2",
"extraAttr12":
[
{
"network12":
{
"allowAction": "yes",
"ipAddresses":
[
"192.168.29.193",
"192.168.29.197"
],
"disabled": false,
"ip_protocol": "IPV4_IPV6"
}
}
]
}
},
{
"policyName3": {
"resourceType": "name3Type",
"id": "uuid-0000-000-0-3",
"extraAttr2":
[
{
"network13":
{
"allowAction": "yes",
"ipAddresses":
[
"192.168.29.191",
"192.168.29.195"
],
"disabled": false,
"ip_protocol": "IPV4_IPV6"
}
}
]
}
}
]
如您所见,这是一个正常 json。我需要通过 id select(提取)整个对象。例如,当我输入 id "uuid-0000-000-0-3" 时,我想接收对象 PolicyName3
{
"policyName3": {
"resourceType": "name3Type",
"id": "uuid-0000-000-0-3",
"extraAttr3":
[
{
"netowork13":
{
"allowAction": "yes",
"ipAddresses":
[
"192.168.29.191",
"192.168.29.195"
],
"disabled": false,
"ip_protocol": "IPV4_IPV6"
}
}
]
}
}
数字是软件自动添加的,我无法删除。
也可以添加额外的 IP 地址 childName.ipAddresses??
感谢您的帮助
Q: "Select (extract) the whole object by id."
A:给定变量policy_list
下面的任务
- set_fact:
ext1: "{{ policy_list|
map('dict2items')|list|flatten|
json_query(query)|
items2dict }}"
vars:
id: "uuid-0000-000-0-3"
query: "[?value.id == '{{ id }}']"
- debug:
var: ext1
给予
"ext1": {
"policyName3": {
"extraAttr2": [
{
"network13": {
"allowAction": "yes",
"disabled": false,
"ipAddresses": [
"192.168.29.191",
"192.168.29.195"
],
"ip_protocol": "IPV4_IPV6"
}
}
],
"id": "uuid-0000-000-0-3",
"resourceType": "name3Type"
}
}
我从下面的变量中得到了 json。
[
{
"policyName1": {
"resourceType": "Name1Type",
"id": "uuid-0000-000-0-1",
"extraAttr1":
[
{
"network11":
{
"allowAction": "yes",
"ipAddresses":
[
"192.168.29.193",
"192.168.29.196"
],
"disabled": false,
"ip_protocol": "IPV4_IPV6"
}
}
]
}
},
{
"policyName2": {
"resourceType": "name2Type",
"id": "uuid-0000-000-0-2",
"extraAttr12":
[
{
"network12":
{
"allowAction": "yes",
"ipAddresses":
[
"192.168.29.193",
"192.168.29.197"
],
"disabled": false,
"ip_protocol": "IPV4_IPV6"
}
}
]
}
},
{
"policyName3": {
"resourceType": "name3Type",
"id": "uuid-0000-000-0-3",
"extraAttr2":
[
{
"network13":
{
"allowAction": "yes",
"ipAddresses":
[
"192.168.29.191",
"192.168.29.195"
],
"disabled": false,
"ip_protocol": "IPV4_IPV6"
}
}
]
}
}
]
如您所见,这是一个正常 json。我需要通过 id select(提取)整个对象。例如,当我输入 id "uuid-0000-000-0-3" 时,我想接收对象 PolicyName3
{
"policyName3": {
"resourceType": "name3Type",
"id": "uuid-0000-000-0-3",
"extraAttr3":
[
{
"netowork13":
{
"allowAction": "yes",
"ipAddresses":
[
"192.168.29.191",
"192.168.29.195"
],
"disabled": false,
"ip_protocol": "IPV4_IPV6"
}
}
]
}
}
数字是软件自动添加的,我无法删除。
也可以添加额外的 IP 地址 childName.ipAddresses??
感谢您的帮助
Q: "Select (extract) the whole object by id."
A:给定变量policy_list
下面的任务
- set_fact:
ext1: "{{ policy_list|
map('dict2items')|list|flatten|
json_query(query)|
items2dict }}"
vars:
id: "uuid-0000-000-0-3"
query: "[?value.id == '{{ id }}']"
- debug:
var: ext1
给予
"ext1": {
"policyName3": {
"extraAttr2": [
{
"network13": {
"allowAction": "yes",
"disabled": false,
"ipAddresses": [
"192.168.29.191",
"192.168.29.195"
],
"ip_protocol": "IPV4_IPV6"
}
}
],
"id": "uuid-0000-000-0-3",
"resourceType": "name3Type"
}
}