0

I need to obtain two values from JSON response data returned from a Rest Assured modelled request:

Request
 public void getCustomerStatuses() {
 Response response =
 given().header("X-AC-User-ID","test-user").
 spec(customerApiSpec).
 when().
 get("/api/v6/status/" + ref + "/").
 then().
 assertThat().statusCode(200).extract().jsonPath();
 String customerStatus =jsonPath.getString("$.cust[?(@.name=='STATUS_ID')].id");
 }

Response:

{
"count": 4,
"cust": [
 {
 "id": "029384",
 "type": "STATUS",
 "name": "STATUS_ID"
 
 },
 {
 "id": "938736",
 "type": "RENEWAL",
 "name": "RENEWAL_ID"
 }
]
}

This throws and java.lang.IllegalArgumentException: Invalid JSON expression:Script1.groovy: 1: Unexpected input: '$.cust[?' @ line 1, column 36. $.cust[?(@.name=='STATUS_ID')].id

What is the correct, best way of obtaining these? I'm aware I can chain extract().response().jsonPath(); off the request but not sure how I can obtain >1 value

asked Jul 8, 2020 at 9:21
2
  • could you add sample response Commented Jul 8, 2020 at 9:32
  • @PDHide yes that would've been helpful! now done, thank you Commented Jul 8, 2020 at 9:37

1 Answer 1

1

JsonPath that is used in RestAssured uses the different syntax. Your path would look like cust.findAll{i -> i.name == 'STATUS_ID' || i.name == 'RENEWAL_ID'}.id. Here is the example code:

package click.webelement.api.restassured;
import io.restassured.path.json.JsonPath;
import java.util.List;
public class ExtractList {
 static final String JSON = "{\n" +
 "\"count\": 4,\n" +
 "\"cust\": [\n" +
 " {\n" +
 " \"id\": \"029384\",\n" +
 " \"type\": \"STATUS\",\n" +
 " \"name\": \"STATUS_ID\"\n" +
 " \n" +
 " },\n" +
 " {\n" +
 " \"id\": \"938736\",\n" +
 " \"type\": \"RENEWAL\",\n" +
 " \"name\": \"RENEWAL_ID\"\n" +
 "\n" +
 " }\n" +
 "]\n" +
 "}";
 public static void main(String[] args) {
 JsonPath jsonPath = new JsonPath(JSON);
 List<String> results = jsonPath.getList("cust.findAll{i -> i.name == 'STATUS_ID' || i.name == 'RENEWAL_ID'}.id");
 for(String result: results){
 System.out.println(result);
 }
 }
}
answered Jul 8, 2020 at 11:14
5
  • that would explain it, thank you. I can extract the Response Body as a JsonPath object as the amended question shows, but this throws a Failed to parse the JSON document error Commented Jul 8, 2020 at 11:38
  • I think changing the question after the answer has been given is a bad idea. You change only your code but rest of your question remains inconsistent. I would revert your changes back and raise new question. Commented Jul 8, 2020 at 12:04
  • BTW what does String.valueOf(response) return? Commented Jul 8, 2020 at 12:06
  • @ Alexey R. Fair point, not a great idea. The return value of String.valueOf(response) is just a Json Object io.restassured.path.json.JsonPath@3d0035d2 Commented Jul 8, 2020 at 12:18
  • 1
    Ah, I have it. Thank you for your help, much appreciated Commented Jul 8, 2020 at 12:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.