How to get first element from jsonpath using index in rest-assured framework?

user2884776 :

My api response:

{
    "123": {
        "userId": 424,
        "firstName": "abc",
        "lastName": "xyz",
        "username": "abc",
        "email": "[email protected]",
        "status": 1
    },
    "234": {
        "userId": 937,
        "firstName": "xyz",
        "lastName": "abc",
        "username": "xyz",
        "email": "[email protected]",
        "status": 0
    },
    and so on ..
}

My code goes like this:

import groovy.inspect.swingui.GeneratedBytecodeAwareGroovyClassLoader;
import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

@Test
public void getUserIdTest() throws IOException, ParseException, SQLException {

String baseUrl = readPropertiesFile().getProperty("baseUrl");
        RestAssured.baseURI = baseUrl;
        RequestSpecification httpRequest = RestAssured.given();
        Response response = httpRequest.request(Method.GET, "myApiPath");
        JsonPath jsonPathEvaluator = response.getBody().jsonPath();

// Now after this, I want to get the value of the userId in the first nested json. I can't use the string "123" e.g. jsonPathEvaluator.get("123.userId") since it is dynamic in nature.
}

Please help me in finding the userId in the first nested json using index or any other way. Thanks in advance!

Vamsi Ravi :

I tried but no luck within RestAssured JsonPath library.

Alternatively I used the org.json library to parse the json string to JSONObject and Iterator to get the index of the first element. Below code snippet is tested with json string you provided.

import groovy.inspect.swingui.GeneratedBytecodeAwareGroovyClassLoader;
import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.path.json.JsonPath;
import org.json.JSONObject;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.testng.annotations.Test;

import junit.framework.Assert;

@Test
public void getUserIdTest() throws IOException, ParseException, SQLException {

        String baseUrl = readPropertiesFile().getProperty("baseUrl");
        RestAssured.baseURI = baseUrl;
        RequestSpecification httpRequest = RestAssured.given();
        Response response = httpRequest.request(Method.GET, "myApiPath");
        String jsonResponseString = response.getBody().asString();
        //String jsonResponseString = "{\n    \"123\": {\n        \"userId\": 424,\n        \"firstName\": \"abc\",\n        \"lastName\": \"xyz\",\n        \"username\": \"abc\",\n        \"email\": \"[email protected]\",\n        \"status\": 1\n    },\n    \"234\": {\n        \"userId\": 937,\n        \"firstName\": \"xyz\",\n        \"lastName\": \"abc\",\n        \"username\": \"xyz\",\n        \"email\": \"[email protected]\",\n        \"status\": 0\n    }\n}";
        JSONObject jsonObject = new JSONObject(jsonResponseString);
        Iterator<String> keys = jsonObject.keys();

        String firstkey =keys.next(); 

        JSONObject jsonObjectElement = new JSONObject( jsonObject.get(firstkey).toString());
        String userId = jsonObjectElement.get("userId").toString();
        Assert.assertEquals(424, Integer.parseInt(userId));

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=151435&siteId=1