我有以下几点:
{
"additionalInfo": [],
"id": "8d929134-0c71-48d9-baba-28fb5eab92f2",
"instanceTenantId": "62f4c8ab6a041c1c090f7994",
"name": "Global",
"siteHierarchy": "8d929134-0c71-48d9-baba-28fb5eab92f2",
"siteNameHierarchy": "Global"
},
{
"additionalInfo": [
{
"attributes": {
"addressInheritedFrom": "0d5e4dc7-ce4e-4aff-bf9f-bb8242797dce",
"type": "area"
},
"nameSpace": "Location"
}
],
"id": "1720c283-a903-43f9-996e-a25d63409d69",
"instanceTenantId": "62f4c8ab6a041c1c090f7994",
"name": "SOUTH SOUTH",
"parentId": "90483b39-e1b3-4135-b1b9-3b7b94d5b2f5",
"siteHierarchy": "8d929134-0c71-48d9-baba-28fb5eab92f2/0d5e4dc7-ce4e-4aff-bf9f-bb8242797dce/90483b39-e1b3-4135-b1b9-3b7b94d5b2f5/1720c283-a903-43f9-996e-a25d63409d69",
"siteNameHierarchy": "Global/MY NETWORK/BRANCH SITES/SOUTH SOUTH"
},
{
"additionalInfo": [
{
"attributes": {
"floorIndex": "1",
"imageURL": "",
"isCadFile": "false",
"rfModel": "103103"
},
"nameSpace": "mapsSummary"
},
{
"attributes": {
"height": "15.0",
"length": "100.0",
"offsetX": "0.0",
"offsetY": "0.0",
"width": "100.0"
},
"nameSpace": "mapGeometry"
},
{
"attributes": {
"address": "Lisbon",
"addressInheritedFrom": "d518b161-85f7-434c-91ae-3eface9cc1d5",
"type": "floor"
},
"nameSpace": "Location"
}
],
"id": "33e64ab2-641b-4778-adcd-95c832199f47",
"instanceTenantId": "62f4c8ab6a041c1c090f7994",
"name": "FLOOR-1",
"parentId": "d518b161-85f7-434c-91ae-3eface9cc1d5",
"siteHierarchy": "8d929134-0c71-48d9-baba-28fb5eab92f2/0d5e4dc7-ce4e-4aff-bf9f-bb8242797dce/90483b39-e1b3-4135-b1b9-3b7b94d5b2f5/aef6ef8c-d3ba-4a1c-a0e7-fad4e00e1c5f/d518b161-85f7-434c-91ae-3eface9cc1d5/33e64ab2-641b-4778-adcd-95c832199f47",
"siteNameHierarchy": "Global/MY NETWORK/BRANCH SITES/OEIRAS/ALGES/FLOOR-1"
}
字符串
我想做的是以这样一种方式过滤json查询,即仅当存在属性“type”时才获得“id”值:“地板”
最初,我尝试至少只保存属性,如果类型等于地板使用以下没有成功(在其他选项中):
- name: Save results to variable
set_fact:
my_test: "{{ results.dnac_response | json_query(myquery) }}"
vars:
myquery: "response[*].additionalInfo[*].attributes[? type=='floor].type"
型
但是,理想情况下,我想要的是在上述条件下获得一个“id”值列表。有什么想法吗?先谢谢你。
补充评论
用这个:
- name: Save results to variable 2
set_fact:
my_test: "{{ results.dnac_response | json_query(myquery) }}"
vars:
myquery: "response[*].{ID: id, Attributes: additionalInfo[*].attributes.type}"
型
我设法得到了这个:
{
"my_test": [
{
"Attributes": [],
"ID": "8d929134-0c71-48d9-baba-28fb5eab92f2"
},
{
"Attributes": [
"area"
],
"ID": "1720c283-a903-43f9-996e-a25d63409d69"
},
{
"Attributes": [
"floor"
],
"ID": "33e64ab2-641b-4778-adcd-95c832199f47"
},
...output omitted
型
但我仍然会过滤它,只在属性为“floor”时获取“ID”,并创建一个可以与循环一起使用的列表。我现在很小,所以任何帮助都将不胜感激。
1条答案
按热度按时间kwvwclae1#
根据数据
字符串
创建字典
型
给予
型
选择id
型
给予
型
型
型
给予
型
然后,下面的表达式给出了相同的结果
型