1
“Output”: [
        {

        "id_": “123”
        "first_name": “Testing”,
        "last_name": “Validation”,
        "state": “VA”,
        “yearofbirth”: 1983,
         "currentcompany”: null
    },
    {
        "id": “456”
        "first_name": “Testing”,
        "last_name": “Validation”,
        "state": “VA”,
        “yearofbirth”: 1975,
         "currentcompany”: null        
},
    {
        id": “456”
        "first_name": “Testing”,
        "last_name": “Validation”,
        "state": “VA”,
        “yearofbirth”: 1990,
        "current_employment": {
         “company number”: 3455,
                   "name": “XYZ Company”,
    }
}

consider the above is the response. I want to validate "id, firstname, last name, state, yearofbirth, current employment is present in response using an array list using a loop.

The problem is I am able to get the value of every key and validate. but unable to take only "Key" and check if its present.

6
  • "I am able to get the value of every key and validate. but unable to take only "Key"". It seems confusing. Can you exemplify (on the question) what type of assertion you want to make? Commented Jun 3, 2020 at 17:55
  • Okey, here is an explanation; using this List<Map<String, String>> outputjson = response.jsonPath().getList("Output"); Next line prints the ID (which i call Key) of 123 (which i call value in my example). System.out.println("*************" + resultjson.get(0).get("id")); But, i want to check and assert that the only "ID" (i.e Key) is present in JSON response.
    – Madhi
    Commented Jun 3, 2020 at 18:01
  • Why u want to validate key
    – PDHide
    Commented Jun 3, 2020 at 18:21
  • Just try taking the key. If you have succeeded that the key exists (any value is returned except of probably null).
    – Alexey R.
    Commented Jun 3, 2020 at 18:29
  • I want to validate the only KEY because my values are completely dynamic. some times i would get 50 user details, sometimes, 20, sometimes 2 etc.
    – Madhi
    Commented Jun 3, 2020 at 18:31

2 Answers 2

1

Here is the example that addresses your particular problem:

package click.webelement.api.restassured;

import io.restassured.path.json.JsonPath;
import java.util.HashMap;
import java.util.List;

public class KeyValidation {

    static final String JSON = "[\n" +
            "    {\n" +
            "        \"id_\": \"123\",\n" +
            "        \"first_name\": \"Testing\",\n" +
            "        \"last_name\": \"Validation\",\n" +
            "        \"state\": \"VA\",\n" +
            "        \"yearofbirth\": 1983,\n" +
            "         \"currentcompany\": null\n" +
            "    },\n" +
            "    {\n" +
            "        \"id\": \"456\",\n" +
            "        \"first_name\": \"Testing\",\n" +
            "        \"last_name\": \"Validation\",\n" +
            "        \"state\": \"VA\",\n" +
            "        \"yearofbirth\": 1975,\n" +
            "         \"currentcompany\": null        \n" +
            "    },\n" +
            "    {\n" +
            "        \"id\": \"456\",\n" +
            "        \"first_name\": \"Testing\",\n" +
            "        \"last_name\": \"Validation\",\n" +
            "        \"state\": \"VA\",\n" +
            "        \"yearofbirth\": 1990,\n" +
            "        \"current_employment\": {\n" +
            "          \"company number\": 3455,\n" +
            "          \"name\": \"XYZ Company\"\n" +
            "        }\n" +
            "    }\n" +
            "]";

    public static void main(String[] args) {
        JsonPath jsp = new JsonPath(JSON);
        List<HashMap> dList = jsp.getList("$");
        for(HashMap obj: dList){
            if(!obj.containsKey("first_name")){
                System.err.println("Object {" + obj.toString() + "} does not contain the required key");
            }
        }
    }

}

P.S. - next time please make sure your JSON example is valid and at least can be parsed...

1
  • Thank you, It helps to resolve the problem i had.
    – Madhi
    Commented Jun 3, 2020 at 23:22
0
  1. Create a model of your JSON as a POJO class (containing the id, firstname, last name, state, yearofbirth, current employment fields)
  2. Deserialize your response string into POJO
  3. In case no exception is thrown - you can assume that your model/schema matches the response.

It doesn't guarantee that the values of these fields are valid, but that'd validate that response contains mandatory fields.

Working with JSON as a plain string is not the best idea, due to usability issues and lack of type safety.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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