通过子节点过滤提取父节点

Extract parent node by filtering on child nodes

我需要过滤 JSON 某些子元素并提取父节点 ID。 JSON的一部分:

[
  {
    "data": {
      "id": "2da44298-05ec-4bb5-acce-b524ef56328c",
        "attributes": {
        "units": [
          {
            "id": "1492de82-2f36-43bf-b077-5cf54a3f38b9",
            "show_name": false,
            "children": [
              {
                "id": "a5d76efa-5b21-4874-a8a5-c9c9f8317ee6",
                "contents": [
                  {
                    "id": "b96c127c-6a4f-4a29-924d-63f0ba55972a",
                    "link": {
                      "url": "#",
                      "target": "_blank",
                        "data": {
                        "aspect-ratio": null
                      }
                    },
                    "duration": null
                  },
                  {
                    "id": "dbb7e8fd-aa35-4acc-8ad7-1c7dcd08a6d8",
                      "link": {
                      "data": {
                        "id": "dbb7e8fd-aa35-4acc-8ad7-1c7dcd08a6d8",
                        "aspect-ratio": null
                      }
                    },
                   "duration": null
                  }
                ]
              },
              {
                "id": "8a805cd0-7447-4fac-b4fc-aaa9a2f7e649",
                "contents": [
                  {
                    "id": "d64138b6-5195-48b4-a0f7-b087c5496587",
                      "link": {
                        "data": {
                        "id": "d64138b6-5195-48b4-a0f7-b087c5496587",
                        "aspect-ratio": null
                      }
                    },
                    "duration": null
                  },
                  {
                    "id": "392406b1-fa20-413b-a98a-4d1a5b201d8e",
                      "link": {
                      "url": "#",
                      "target": "_blank",
                     "data": {
                        "id": "423498d9-8e0f-41ef-891a-34b078862ce7",
                        "aspect-ratio": null
                      }
                    },
                    "duration": null
                  }
                ]
              }
            ],
            "contents": []
          }
        ],
        "text": []
      }
    },
    "jsonapi": {
      "version": "1.0"
    }
  }
]

例如,需要提取内容id过滤的单元id b96c127c-6a4f-4a29-924d-63f0ba55972

我尝试了以下表达式:

$..data..?(@contents.data.id == 'b96c127c-6a4f-4a29-924d-63f0ba55972a')].id

$..data..?(@contents.data.id == 'b96c127c-6a4f-4a29-924d-63f0ba55972a')].children.id

$..data..?(@contents.data.id == 'b96c127c-6a4f-4a29-924d-63f0ba55972a')].unit.id

我需要这样做,因为这些 ID 是从不同的响应中获取的。

您可以使用嵌套 filter operators 来获取父节点,例如:

$..data.attributes.units.[?(@.children[?(@.content[?(@.id == 'b96c127c-6a4f-4a29-924d-63f0ba55972')])])].id

演示:

更多信息:JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios