0

My API's response is something like this: JSON

"Catalog": 
[
        {
            "longDescription": "some long description",

        "Price": [
            {
                "priceValue": "60.0",
            }
        ],
        "name": "some name",
       and so on.

I want to get the price value and I am using JsonPathEvaualtor.setRoot("catalog")? do I use ("price") or is there something else used to traverse inside catalog array then inside that price array.

Sinto
  • 3,915
  • 11
  • 36
  • 70

2 Answers2

0

So basically the parsing structure of your json with rest assured is this to get "Price" array

List<Map<String, List<Map<String, String>>>>

Here is the solution

First set the root to catalog

JsonPathEvaualtor.setRoot("catalog")

To get the priceValue correspodning to the first map element in the array, do the following

response.jsonPath().getList("$").getMap("Price").get(0).getMap(0).getString("priceValue");

I am not sure whether the above code is without bugs as I have not executed it. Hope it gives you some idea about the direction of the solution

Refer this tutorial for further examples ono parsing lists and maps in rest assured response https://www.testingexcellence.com/parse-json-response-rest-assured/

Aarish Ramesh
  • 6,745
  • 15
  • 60
  • 105
0

I would suggest de-serialize your json to a POJO and then use the getters to extract information from the json especially when the json is little complicated [having lists and nested objects].

RestAssured provides option for de-serialization. There are other libraries like gson and jackson for the same. Personally I find gson much easy to use. Have a look at the below links for more insights.

https://futurestud.io/tutorials/gson-mapping-of-arrays-and-lists-of-objects

https://github.com/rest-assured/rest-assured/wiki/usage#deserialization