Did you ever wonder why sometimes you have json with curly brackets while sometimes with square brackets? This is because you were dealing with JSON object and arrays respectively. JSON array organizes collection of related items which can be json object in themselves -
[{"name":"item 1"},{"name": "item2} ]
JSON object usually contains key/value pair of related item. For ex -
{"name": "firstname", "dateOfBirth":"65475645"}
We used Jackson parser in API video tutorial but if you don’t need entire data set from json response of API call then you can parse the API response using JSONArray or JSONObject APIs.
you would use JSONArray to parse JSON which starts with the array brackets. On the other hand, you would use JSONObject when dealing with JSON that begins with curly braces.
Of course, JSON arrays and objects may be nested inside one another. One common example of this is an API which returns a JSON object containing some metadata alongside an array of the items matching your query:
{"startIndex": 0, "data": [{"name":"item 1"},{"name": "item2"} ]}
In case httpbin goes down in future, the same json looks as -
{
- args:
- {
- show_env: "1"
- },
- headers:
- {
- Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
- Accept-Encoding: "gzip, deflate, sdch, br",
- Accept-Language: "en-US,en;q=0.8,de;q=0.6",
- Host: "httpbin.org",
- Runscope-Service: "httpbin",
- Upgrade-Insecure-Requests: "1",
- User-Agent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36",
- X-Forwarded-For: "94.135.236.134",
- X-Forwarded-Protocol: "https",
- X-Forwarded-Ssl: "on",
- X-Real-Ip: "94.135.236.134"
- },
- origin: "94.135.236.134",
}
// get JSON response
Client client = Client.create();
WebResource webResource = client.resource("https://httpbin.org/get?show_env=1");
ClientResponse clientResponse = webResource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
System.out.println(clientResponse.getStatus());
String jsonResponse = clientResponse.getEntity(String.class);
System.out.println(jsonResponse);
Since the output is json object here, we will use JSONObject to parse the response from previous example and print the data we need.
// parse JSON objects
JSONObject jsonObject = new JSONObject(jsonResponse);
System.out.println("headers are: "+jsonObject.getString("headers"));
System.out.println("origin is: "+jsonObject.getString("origin"));
System.out.println("url is: "+jsonObject.getString("url"));
I am just printing the required data points but you can use it carry assert operation to verify if data returned is valid or not.
Now let’s see an example of parsing json array. For this we would use the API call. In case the api goes down in future, this is how the response looks like -
{
- Content-Length: "137",
- Content-Type:
- [
- "application/json",
- "text/plain; charset=UTF-8"
- ],
- Server: "httpbin"
}
it is a JSON object which contains “Content-Type:” array and we will print all of its values.
// get JSON response
webResource = client.resource("https://httpbin.org/response-headers?Content-Type=text%2Fplain%3B+charset%3DUTF-8&Server=httpbin");
clientResponse = webResource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
System.out.println(clientResponse.getStatus());
jsonResponse = clientResponse.getEntity(String.class);
System.out.println(jsonResponse);
// parse JSON Array
jsonObject = new JSONObject(jsonResponse);
JSONArray jsonArray = jsonObject.getJSONArray("Content-Type");
System.out.println("Content-Type json array is: "+jsonArray);
for(int index=0; index<jsonArray.length(); index++) {
System.out.println("Content at JSON array index: "+index+", "+ jsonArray.get(index));
}