0

I want to verify that id: 1 belongs to Tiger Nixon

{"status":"success","data":[{"id":"1","employee_name":"Tiger Nixon","employee_salary":"320800","employee_age":"61","profile_image":""}

I have been following this document: https://github.com/rest-assured/rest-assured/wiki/Usage#json-using-jsonpath , this section body("shopping.category.find { it.@type == 'groceries' }.item", hasItems("Chocolate", "Coffee"));

Currently I am getting a null pointer exception. I could use some help to come up with a solution. Thanks in advance for your time.

import com.google.gson.JsonObject;
import io.restassured.RestAssured;
import org.testng.Assert;

public class RestAssuredExample_2 {

    public void getResponse() {
        JsonObject responseObject = RestAssured.get("http://dummy.restapiexample.com/api/v1/employees")
                .then()
                .extract().response().as(JsonObject.class);

        String emp1Name = responseObject.get("data.find{it.@id=='1'}.employee_name").toString();
        Assert.assertEquals(emp1Name, "Tiger Nixon");
    }

    public static void main(String[] args) {
        RestAssuredExample_2 rest2 = new RestAssuredExample_2();
        rest2.getResponse();
    }
}  

Error:

Exception in thread "main" java.lang.NullPointerException
    at HTTPz.RestAssuredExample_2.getResponse(RestAssuredExample_2.java:16)

1 Answer 1

1

Tried to replicate this scenario

String responseObject = RestAssured.get("http://dummy.restapiexample.com/api/v1/employees").then().extract().asString();
JsonPath js = new JsonPath(responseObject);
String emp1Name = js.get("data.find {it.id =='1'}.employee_name").toString();
System.out.println(emp1Name);

And I get the value for emp1Name as "Tiger Nixon"

Difference :

Original : it.@id=='1'

Mine : it.id =='1'

It seems like @ is for XMLPath and not for JSONPath and honestly I wouldn't know about it in detail as well cause I have been using JSONPath only for a very long time :)

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.