Home > OS >  could not fetch json object using jsonpath
could not fetch json object using jsonpath

Time:01-03

I've a requirement to fetch value from json using jayway jsonpath.

Json structure looks like below

[
  {
    "type": "a",
    "values": [
      {
        "name": "a",
        "value": [1,2,3]
      },
      {
        "name": "b",
        "value": [3,4,5]
      },
      {
        "name": "c",
        "value": [6,7,8]
      }
    ]
  }
]

my requirement is in the values array if the name value is a and value array contains value 1, then I need to fetch value array where name is b.

I have written jsonPath expression like below

$..values[?(@.name == 'a')]

where it is returning only

{
  "name": "a",
  "value": [1,2,3]
}

could someone help me in writing jsonpath expression please , Thanks in advance.

expected output

[3,4,5]

tried with

$..[?(@.values[?(@.name== 'a' && @.value CONTAINS 1)])]

then it is matching every object present in root array.

CodePudding user response:

With Jayway JSONPATH you might get lucky with below jsonpath

$..[?(@.values[?(@.name=='a')].value[*] contains 1 )].values[?(@.name=='b')].value[*]
  • Related