Search code examples
javajsonrestrest-assuredjsonparser

How to fetch value from api response using rest Assured


I am new to rest assured. I have a response json as below. where I have multiple nested json in an array having details of spends on particular Merchant. I need to get the spend value of Netflix only. How can I check for the condition where it is Netflix get the absolute value for that only ?

{
"data": {
    "content": [
        {
            JsonPath(response);
js.get(key).toString();

I used to get value directly using JsonPath but now it is a condition based value I need to fetch.

Here I need to fetch the value of key "absolute" where the merchantBrandName is Netflix, which is again inside a nested json


Solution

  • There are 2 options for you.

    1. Using com.jayway.jsonpath.JsonPath
    List<Double> read = com.jayway.jsonpath.JsonPath.read(res, "$.data.content[?(@.element.merchantBrandName == 'Netflix')].absolute");
    System.out.println(read.get(0));
    
    //2170.220006465912
    
    1. Using io.restassured.path.json.JsonPath
    double abs = JsonPath.from(res).getDouble("data.content.find {it.element.merchantBrandName == 'Netflix'}.absolute");
    System.out.println(abs);
    //2170.22 
    //I don't have time to figure out why it's getting rounded