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

  • could you add sample response – PDHide Jul 8 at 9:32
  • @PDHide yes that would've been helpful! now done, thank you – Steerpike Jul 8 at 9:37
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);
        }

    }

}
| improve this answer | |
  • 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 – Steerpike Jul 8 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. Jul 8 at 12:04
  • BTW what does String.valueOf(response) return? – Alexey R. Jul 8 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 – Steerpike Jul 8 at 12:18
  • 1
    Ah, I have it. Thank you for your help, much appreciated – Steerpike Jul 8 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.