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
-
could you add sample responsePDHide– PDHide2020年07月08日 09:32:57 +00:00Commented Jul 8, 2020 at 9:32
-
@PDHide yes that would've been helpful! now done, thank youSteerpike– Steerpike2020年07月08日 09:37:07 +00:00Commented Jul 8, 2020 at 9:37
1 Answer 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);
}
}
}
-
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
errorSteerpike– Steerpike2020年07月08日 11:38:38 +00:00Commented 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.Alexey R.– Alexey R.2020年07月08日 12:04:39 +00:00Commented Jul 8, 2020 at 12:04
-
BTW what does
String.valueOf(response)
return?Alexey R.– Alexey R.2020年07月08日 12:06:19 +00:00Commented 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 Objectio.restassured.path.json.JsonPath@3d0035d2
Steerpike– Steerpike2020年07月08日 12:18:07 +00:00Commented Jul 8, 2020 at 12:18 -
1Ah, I have it. Thank you for your help, much appreciatedSteerpike– Steerpike2020年07月08日 12:21:45 +00:00Commented Jul 8, 2020 at 12:21