3

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.

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

is quite similar to JSONpath so perhaps a jq solution will be of help. It is, at least, very simple:

# find all the docNames for which the attr1 == true
.DATA[]
| select( any(.result[]; .attribute == "attr1" and .value == true) )
| .docName

Java bindings

1 Comment

See update, but please note that I have no personal experience with any of these. Let us know about any pros and cons that you encounter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.