1

How to handle when REST service response is returned as Json Array: for e.g:

[{"boolField":true,"intField":991, "StringField":"SampleString"},
 {"boolField":false, "intField":998, "StringField": "SampleString2"}]";

All the blogs & examples I have seen didn't deal with the scenario mentioned above. For example, I went through http://goessner.net/articles/JsonPath/ expression syntax, but couldn't locate the solution.

For example the following json, if I want to get all the authors, I can use $.store.book[*].author or $..author

{ "store": {
    "book": [ 
      { "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      { "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      }]}
}

However, if the REST service response is returned like below, then how to get all authors from the list

[
  { "category": "reference",
    "author": "Nigel Rees",
    "title": "Sayings of the Century",
    "price": 8.95
  },
  { "category": "fiction",
    "author": "Evelyn Waugh",
    "title": "Sword of Honour",
    "price": 12.99
   }
]

2 Answers 2

3

$..author will retrieve all of the authors.

In general when your root object is an array, you can think of the $ as an array and do things like $[0] to inspect the first item (You'll still get an array back however).

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

3 Comments

I tested it at jsonpath.curiousconcept.com and got the array of authors back, what results do you get?
java.lang.IllegalArgumentException: The parameter "author" was used but not defined. Define parameters using the JsonPath.params(...) function when I use $..author. If i use $.author an empty [] is returned. If I use $ alone, I am getting the list.
I tried in jsonpath.curiousconcept.com, and it resulted in An unknown error has occured. I am at least able to proceed and not blocked, as I am getting $ result as list.
0

In my case [0].author worked (I left out the $).

Comments

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.