简体   繁体   中英

Jsonpath: get the value of an element based on its sibling, when the sibiling element is an array containing a specific value

With the following json:

{
  "elements": [
    {
      "ids": [
        {
          "id": "A",
        },
        {
          "id": "B",
        }
      ],
      "value": "one"
    },
    {
      "ids": [
        {
          "id": "D",
        },
        {
          "id": "E",
        }
      ],
      "value": "two"
    }
  ]
}

What would be the jsonpath to return the value one when asking for the id A ?

As per https://stackoverflow.com/a/47576707 I can retrieve the ids element containing A :

$.elements.*.ids[?(@.id=='A')] or $..ids[?(@.id=='A')]

with result:

[
   {
      "id" : "A"
   }
]

but I would like to access the value of its sibling ( "value": "one" ).

Thanks in advance!

You can also use the in filter operator .

$.elements[?('A' in @.ids.*.id)].value

jsonpath:

$.elements[?(@.ids.*.id contains 'A')].value

result:

[
   "one"
]

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM