4

I have response from RestAssured which is a JsonArray looks similar to the below code

[{ "id": "1", "applicationId": "ABC" }, { "id": "2", "applicationId": "CDE" }, { "id": "3", "applicationId": "XYZ" }]

I use code to get the "id" from the first Json element

    List<String> jresponse = response.jsonPath().getList("$");
    for (int i = 0; i < jsonResponse.size(); i++) {
    String id  = response.jsonPath().getString("id[" + i + "]");
    if(id.equals("1"){
        do something
    }
    else {
        something else
    }

    }

Is there a way to use foreach in place of for in the above code?

5
  • Did you use jackson for work with POJO? Commented Jul 7, 2019 at 19:53
  • Were you able to check my answer? Commented Jul 10, 2019 at 10:00
  • @Fenio The question is how to parse the response directly using foreach. The "Identity" class example what you have provided is not what I wanted. Commented Jul 11, 2019 at 12:23
  • @MelvinRichard I'll try again then Commented Jul 11, 2019 at 12:25
  • @MelvinRichard Check my updated answer again please Commented Jul 11, 2019 at 12:31

1 Answer 1

7

Instead of getting the root level like this:

List<String> jresponse = response.jsonPath().getList("$");

You can grab IDs directly:

List<String> ids = path.getList("id");

Then, you can use foreach loop, instead of using indexes like this:

        List<String> ids = path.getList("id");
        for (String id : ids) {
            if (id.equals("1")) {
                //do something
            } else {
                //do something else
            }
        }

EDIT:

The best way (probably) is to create objects representing the JSON. In order to do that we have to understand what JSON contains. So far, you have JSON Array which contains JSON Objects. Each of JSON Object contain id and applicationId. In order to parse this JSON into a Java class we have to create a class. Let's call it Identity. You can call it whatever you want.

public class Identity {
    public String id;
    public String applicationId;
}

The above is the representation of JSON Object. Field names are exact names in JSON. Identifiers should be public.

Now, to parse JSON into Java classes we can use JsonPath like this:

Identity[] identities = path.getObject("$", Identity[].class);

Then, we iterate over the array to get what we want:

        for (Identity identity : identities) {
            if (identity.id.equals("1")) {
                System.out.println(identity.applicationId);
            }
        }

Based on that you can create a full method instead of just printing the applicationId like this:

    private static String getApplicationId(String id, Identity[] identities) {
        for (Identity identity : identities) {
            if (identity.id.equals(id)) {
                return identity.applicationId;
            }
        }
        throw new NoSuchElementException("Cannot find applicationId for id: " + id);
    }

ANOTHER EDIT:

In order to use foreach and get applicationID based on id you need to use getList method but in different manner.

List<HashMap<String, String>> responseMap = response.jsonPath().getList("$");

In the above code, we get the list of JSON Objects in the JSON Array.

Each of the elements in the HashMap is a single JSON Object. Strings are the attributes like id and applicationId and second String are the values of each attribute.

Now, we can use foreach loop like this to get desired results:

private static String getApplicationIdBasedOnId(Response response, String id) {
    List<HashMap<String, String>> responseMap = response.jsonPath().getList("$");
    for (HashMap<String, String> singleObject : responseMap) {
        if (singleObject.get("id").equals(id)) {
             return singleObject.get("applicationId");
        }
    }
    throw new NoSuchElementException("Cannot find applicationId for id: " + id);
}
Sign up to request clarification or add additional context in comments.

4 Comments

The question is if the id is equal to 1, then how to get the particular applicationId?
@MelvinRichard Give me 5 minutes, I'll update my answer
@Fenio - what if the response is just a simple array of numbers ? Can I get the root element directly as a Java array ?
@MasterJoe2 you can get any root element with $ using Rest Assured's JSON path. List<Integer> numbers = path.getList("$");

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.