0

I am creating sample JSON as shown below. I want to retrieve value by using on certain condition.

Observe below line of statement:

'$.Attributes..Attribute[?(@.setcode=="x")]'

But I am unable to retrieve the value by using code.

"{
  "Attributes": [
    {
      "Attribute": {
        "setcode": "x",
        "codeType": "movieType",
        "AttributeGroup": [
          {
            "code": "Force",
            "codeValueValue": "'I'"
          },
          {
            "code": "Remain",
            "codeValueValue": "'P'"
          },
          {
            "code": "Died",
            "codeValueValue": "'E'"
          },
          {
            "code": "Renew",
            "codeValueValue": "'R'"
          }
        ]
      }
      
    },
    {
      "Attribute":{
        "setcode": "y",
        "codeType": "movietype",
        "AttributeGroup": [
          {
            "code": "Force",
            "codeValueValue": "'I'"
          },
          {
            "code": "Remain",
            "codeValueValue": "'P'"
          },
          {
            "code": "Died",
            "codeValueValue": "'E'"
          },
          {
            "code": "Renew",
            "codeValueValue": "'R'"
          }
        ]
      }
    }
  ]
}"
bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • Welcome to Stack Overflow. To get useful answers to your questions, it's helpful to post minimum reproducible examples with code, and to make explicit what you expect, and how what is happening deviates from what you expect, including any error messages. Further, if you are getting an error involving a `parse error near token ?` check out this Github issue: https://github.com/h2non/jsonpath-ng/issues/16 – Katie Aug 20 '20 at 13:20

1 Answers1

0

While your path returns something using a common implementation in Java (Jayway), I can confirm no result is returned using it with jsonpath-ng. The issue is the "filter op can only be used on lists" and, it looks like, requires to start with the iterable beforehand in the path:

We have to filter on the Attributes-array and pull the Attribute afterward instead:

$.Attributes[?(@.Attribute.setcode=="x")].Attribute

Full code example:

import json
json_string = """{
  "Attributes": [
    {
      "Attribute": {
        "setcode": "x",
        "codeType": "movieType",
        "AttributeGroup": [
          {
            "code": "Force",
            "codeValueValue": "'I'"
          },
          {
            "code": "Remain",
            "codeValueValue": "'P'"
          },
          {
            "code": "Died",
            "codeValueValue": "'E'"
          },
          {
            "code": "Renew",
            "codeValueValue": "'R'"
          }
        ]
      }          
    },
    {
      "Attribute":{
        "setcode": "y",
        "codeType": "movietype",
        "AttributeGroup": [
          {
            "code": "Force",
            "codeValueValue": "'I'"
          },
          {
            "code": "Remain",
            "codeValueValue": "'P'"
          },
          {
            "code": "Died",
            "codeValueValue": "'E'"
          },
          {
            "code": "Renew",
            "codeValueValue": "'R'"
          }
        ]
      }
    }
  ]
}"""

json_data = json.loads(json_string)

from jsonpath_ng.ext import parser
query = [x.value for x in parser.parse('$.Attributes[?(@.Attribute.setcode=="x")].Attribute').find(json_data)]
print(json.dumps(query))
wp78de
  • 18,207
  • 7
  • 43
  • 71