1

I have a below JSON. I want to get pin code where street =='abc strt.'
I am using Jaway to achieve this.

{
  "studentId": "10001",
  "studentSection": "A",
  "address": [
    {
      "street": "abc strt.",
      "pincode": "00000000"
    },
    {
      "street": "xzy strt.",
      "pincode": "1111111"
    },
    {
      "street": "678 strt.",
      "pincode": "8765"
    },
    {
      "street": "ity strt.",
      "pincode": "234567"
    }
  ]
}
List<String> = result = JsonPath.read(json, "$.['address'].[[email protected] == 'abc strt.'].pincode");

JayWay XPath: $.['address'].[[email protected] == 'abc strt.'].pincode, it's returning List<String>.
The above query will always return a single value, but due to the return type as List<String>, I have to use value as list.get(0).


Is there a way to return String instead of List??

Also, I have 10 more queries, which I have to execute on the same JSON. Should I query once & use the result, or can I query 10 times and use the result. What is the preferred way to do this?

1 Answer 1

0

As per Jayway Jsonpath docs,

Jayway JsonPath is a Java port of Stefan Goessner JsonPath implementation.

And as per Stefan Goessner JsonPath docs,

Please note, that the return value of jsonPath is an array, which is also a valid JSON structure. So you might want to apply jsonPath to the resulting structure again or use one of your favorite array methods as sort with it.

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

2 Comments

Thanks for the answer. I have one more doubt, is it a good practice to query the json multiple times, or query once & then use the result. For example, similar to the above query, I have 10 more queries. Should I combine them, or individually query is also good performance-wise?
I am not completely sure about the performance, may be you can give it a try - call combined query n number of times and call 10 different queries also n number of times each and measure the times.

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.