过滤JSON查询复杂嵌套列表

rm5edbpk  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(113)

我有以下几点:

{
              "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”,并创建一个可以与循环一起使用的列表。我现在很小,所以任何帮助都将不胜感激。

kwvwclae

kwvwclae1#

根据数据

response:
      - additionalInfo: []
        id: 8d929134-0c71-48d9-baba-28fb5eab92f2
        instanceTenantId: 62f4c8ab6a041c1c090f7994
        name: Global
        siteHierarchy: 8d929134-0c71-48d9-baba-28fb5eab92f2
        siteNameHierarchy: Global
      - additionalInfo:
        - attributes: {addressInheritedFrom: 0d5e4dc7, 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
        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, 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
        siteNameHierarchy: Global/MY NETWORK/BRANCH SITES/OEIRAS/ALGES/FLOOR-1

字符串
创建字典

id_type: "{{ dict(response|
                    json_query('[].[id,
                                    additionalInfo[].attributes.type]')) }}"


给予

id_type:
    1720c283-a903-43f9-996e-a25d63409d69: [area]
    33e64ab2-641b-4778-adcd-95c832199f47: [floor]
    8d929134-0c71-48d9-baba-28fb5eab92f2: []


选择id

my_test: "{{ id_type|dict2items|
               selectattr('value', 'contains', 'floor')|
               map(attribute='key') }}"


给予

my_test:
    - 33e64ab2-641b-4778-adcd-95c832199f47

  • 用于测试的完整剧本示例
- hosts: localhost

  vars:

    response:
      - additionalInfo: []
        id: 8d929134-0c71-48d9-baba-28fb5eab92f2
        instanceTenantId: 62f4c8ab6a041c1c090f7994
        name: Global
        siteHierarchy: 8d929134-0c71-48d9-baba-28fb5eab92f2
        siteNameHierarchy: Global
      - additionalInfo:
        - attributes: {addressInheritedFrom: 0d5e4dc7, 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
        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, 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
        siteNameHierarchy: Global/MY NETWORK/BRANCH SITES/OEIRAS/ALGES/FLOOR-1

    id_type: "{{ dict(response|
                      json_query('[].[id,
                                      additionalInfo[].attributes.type]')) }}"
    my_test: "{{ id_type|dict2items|
                 selectattr('value', 'contains', 'floor')|
                 map(attribute='key') }}"
  tasks:

    - debug:
        var: id_type|to_yaml
    - debug:
        var: my_test

  • 也可以创建列表
id_type: "{{ response|
               json_query('[].{id: id,
                               type: additionalInfo[].attributes.type}') }}"


给予

id_type:
    - id: 8d929134-0c71-48d9-baba-28fb5eab92f2
      type: []
    - id: 1720c283-a903-43f9-996e-a25d63409d69
      type: [area]
    - id: 33e64ab2-641b-4778-adcd-95c832199f47
      type: [floor]


然后,下面的表达式给出了相同的结果

my_test: "{{ id_type|
               selectattr('type',  'contains', 'floor')|
               map(attribute='id') }}"

相关问题