Testing Mavens

Demystifying the JSON Maze

Thu May 30 2024

AN
Arundev Nair
thumbnail

Finding the path in JSON maze.

We all know that api test automation trims down the time taken by automated validation of an application by lion share. But the challenge we face during the script development is how do we get the correct value from the api response. There could be multiple ways to unwind the response to reach your specific validation node and let’s discuss about few among those. Before we proceed, I assume you are a polyglot who understands Java as well.

e.g., 1: String value from JSON response.

First let’s consider a very simple response which a JSONObject with only keys & value (I meant, no child objects). You made the call to the api

String apiUrl = http://superherodirectory.com/getHeros/7003438919

and the response you got is,

{
  "firstName": "John",
  "lastName": "Doe",
  "roleId": "7003438919",
  "emailId": "superman@hollywood.com",
  "isAlive": "immortal"
}

You won’t get easier response than this because, all you have is a JSONObject with the values in String format.

Assume, your intention is to validate the isAlive status.

Well, as it is very common to use RestAssured to make the api calls in automation projects, you would get a ‘response’ of type Response class. Now let’s make use of JSONObject in org.json library to convert this response into a JSONObject.

JSONObject responseObject = new JSONObject(response.asString());

The major task is done. Using the desired key, we can get the corresponding value.

String isAlive = responseObject.get(“isAlive”).toString();

Or we can make it even simpler:

String email = responseObject.getString(“isAlive”); 

Output will be “immortal” in both the cases.

e.g., 2: String value from inner object of a JSON response.

Here your response object is a bit more complex with inner objects and arrays of data.

{
  "Email": {
    "EmailAddress": "superman@hollywood.com",
    "Status": [
      {
        "Name": "SMS",
        "Opted": true
      },
      {
        "Name": "Promotions",
        "Opted": false
      }
    ],
    "Type": "Personal",
    "TypeId": 1
  },
  "MemberSince": "2020-11-09",
  "RoleID": "1012345678",
  "RoleState": "Member",
  "Status": "Active"
}

We need to extract 2 different values from this. Email address & roleId.

Let’s start with getting the response JSONObject as before.

JSONObject responseObject = new JSONObject(response.asString());

The easiest value which we can extract from the response is roleid, very similar to earlier as:

String roleId = responseObject.getString("RoleID");

Another value we need is email id, but as it being an inner object, we need to get the child object first and then extract the string value out of this.

responseObject.get("Email");

This will return an Object which we need to cast to JSONObject so that we can get the child properties using key-value.

JSONObject email = (JSONObject)responseObject.get("Email");

Now as we have the child object, as we did earlier let’s separate out the email address string value.

String emailAddress = email.get("EmailAddress").toString();

Which will result in a String value of “superman@hollywood.com”

Easy there any easy step? Here you go:

String emailAddress = responseObject.getJSONObject("Email").getString("EmailAddress");

e.g., 3: Boolean value from inner JSONArray of a JSON response.

Using the same response from previous example, our next requirement is to get the value of Promotions opted in status which involves navigating through the arrays too.

Here we are going with the assumption that Status array structure remains the same so that Promotions status will always be at second position (or, index# = 1).

To begin with let’s get the Email object first as we did earlier, in which the Status array is present.

JSONObject email = (JSONObject)responseObject.get("Email");

Now let’s get the Status array

JSONArray status = (JSONArray)email.get("Status");

We have to get the nth index json object from array, which is our case is 1.

JSONObject promotions = (JSONObject)status.get(1);

This promotion object holds the value of status for promotion and we can retrieve the value now, which as you can see is a boolean value.

boolean optedIn = (boolean)promotions.get("Opted");

And we get the output as a Boolean value of ‘false’, that’s it. If you are curious about the one liner code, it will be as follows:

boolean optedIn = responseObject.getJSONObject("Email").getJSONArray("Status")                       
.getJSONObject(1).getBoolean("Opted");

 

Background

Your Quality Gatekeepers,

Partner with us today.