SoftwareTestPilot
All discussions
Sam OkaforJul 10

How do I extract a value from a JSON response with REST Assured JsonPath?

Need to chain: POST /login, grab token, use in next request. Cleanest way?

rest-assuredjsonpath
Related learningExplore Academy skill paths

1 answer

Accepted solution

Sam OkaforJul 11

Use `JsonPath` from the response body, or extract inline with `path()`.

java
Response res = given().when().get("/api/orders/1001").then().statusCode(200).extract().response();

String status   = res.jsonPath().getString("status");
int    itemCount = res.jsonPath().getList("items").size();
String firstSku = res.jsonPath().getString("items[0].sku");
List<String> allSkus = res.jsonPath().getList("items.sku");

// filter syntax (Groovy GPath)
String pricey = res.jsonPath().getString("items.find { it.price > 100 }.sku");

Notes that save time: REST Assured uses Groovy GPath, not strict Jayway JsonPath, so `find`, `findAll`, `collect` and `it` all work but `$..` recursive descent does not. Prefer `extract().response()` once and query it many times instead of re-sending the request. For typed extraction map straight to a POJO with `res.as(Order.class)` and assert on fields — it fails louder than string comparison when the schema drifts.