0

I have this test I'm writing in RestAssured. I want to extract the id in String but always returning it as array. This is the test I have written so far.

@Test(priority = 1)
    public static void searchForUsername( ) throws Throwable {
        ValidatableResponse response= given().contentType(ContentType.JSON).queryParam("username",
                "Delphine").log().parameters().get("https://jsonplaceholder.typicode.com/users").then().log().all();

        JsonPath extractor = response.extract().jsonPath();
        userId = extractor.getString("id");
        System.out.println(userId);
    }

It prints it as [9] instead of 9

0

1 Answer 1

1

Reason: the object that you extract is in an array. So when you extract, the id will be in an array.

Solution: From list of id, you can get by index.

@Test
void name2() {
    Response res = RestAssured.given()
            .queryParam("username", "Delphine")
            .get("https://jsonplaceholder.typicode.com/users");
    int id = (Integer) res.jsonPath().getList("id").get(0);
    System.out.println(id);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.