jsonrest-assuredrest-assured-jsonpath

How to filter value from api response using rest Assured


I have a response json as below. where I have multiple nested json in an array having details of spends on particular channel. I need to get the spend value of "EMAIL" only. How can I check for the condition where it is EMAIL get the value for that only ?

Here I need to have two condition where key == "xyhz" get the value and inside that value array/list get the value where channel == "Email". So there should be two conditions.


Solution

  • Your json is multiple nested array

    enter image description here

    So I think JsonPath jayway is better choice in this situation.

    List<?> values = com.jayway.jsonpath.JsonPath.read(response, "$.communicationPolicy..rules[?(@.key == 'TRANSACTIONAL')].value[?(@.channel == 'EMAIL')].value");
    System.out.println(values);
    

    If you still want GPath, then here it is. I use flatten() to make 2D aray to 1D array [[xxx]] --> [xxx]

    List<?> value = JsonPath.from(response).get("communicationPolicy.rules.flatten().findAll {it.key == 'TRANSACTIONAL'}.value.flatten().findAll {it.channel == 'EMAIL'}.value");
    System.out.println(value);