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'].[?@.street == 'abc strt.'].pincode");

JayWay XPath: $.['address'].[?@.street == '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?
mayank bisht
  • 618
  • 3
  • 14
  • 43

1 Answers1

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.

Smile
  • 3,832
  • 3
  • 25
  • 39
  • 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? – mayank bisht Jun 22 '20 at 06:15
  • 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. – Smile Jun 22 '20 at 06:49