Search code examples
jsonjsonpathjson-path-expression

Json Path - Filter on List of List


I have a JSON like this:

{
"DATA": [
  {
    "docName": "xyz",
    "result": [
      {
        "attribute": "attr1",
        "value": true
      },
      {
        "attribute": "attr2",
        "value": true
      }
    ]
  },
  {
    "docName": "abc",
    "result": [
      {
        "attribute": "attr1",
        "value": false
      },
      {
        "attribute": "attr2",
        "value": true
      }
    ]
  }]
}

My use case is to find all the docNames for which the attr1 = true

I tried a couple of filter expressions but I am unable to get the doc names. I tried something like this: $.DATA[?(@.result[0].attribute=='attr1' && @.result[0].value == true)].docName and I get the docNames, but I can't rely on the 1st element of the list. I need to verify if attr1 is present anywhere in the list with value of true.

Any help would be appreciated.


Solution

  • After some coding & then figuring out why this is not working, what exactly I found out is that, currently its not possible with jsonpath to get parent element from filtering child node (nested json). Open Issue Link

    Otherwise this might have worked,

    $.DATA[?(@.result[?(@.attribute=="attr1" && @.value==true)])].docName