Example of iterating through JSON attributes in Gson

By | May 8, 2018

When working with Gson, in case of a JSON object having too many attributes or those attributes not being known upfront, it is not practical to (try to) convert the JSON object to a Java object (via fromJson() method). This situation will occur when you start working on your JSON assignment. The assignment includes calling Currency Converter API service that retrieves a list of countries. The service retrieves the total of 201 countries, but the developers of this API, for a reason unclear to me, decided to store the list of countries not as a JSON array, but as attributes of a JSON object. However, there is a simple approach to parsing countries represented in this form.

First, like we did during the class, we need to fetch the content from this REST service, convert it to a JSON object and get the value of the results attribute. The following example shows how to parse countries from the JSON object that is the value of the results attribute. This object is called countriesJson.

JsonObject countriesJson = ...
Set<Entry<String, JsonElement>> attributeEntres = countriesJson.entrySet();

for (Entry<String, JsonElement> entry : countriesJson.entrySet()) {
    String attributeName = entry.getKey();
    JsonObject attributeValue = (JsonObject) entry.getValue();

    ...
}

Method entrySet() retrieves an object attributeEntres of a class Set. Class Set represents a set of elements, i.e. an unordered collection of elements in Java (compared to a list that represents an ordered collection of elements). A set contains objects of a class Entry that has two fields: key and value. In our case, each Entry instance represents a JSON attribute (corresponding to a country). The key is the name of a JSON attribute (country code), and the value is the value of the JSON attribute (which is a JSON object holding information about the country).

Since set is an unordered collection, we can not access elements via an index (like we do with lists by using the get() method). One approach to iterating over a set is by using the for-each loop. For-each loop performs similar operation as the for loop does, with an important difference where for-each loop iterates throughout the whole collection (unless explicitly interrupted via break). A short description of the for-each loop can be found here. In each iteration, entry references an object being accessed in the current iteration. In our example, by using the getKey() method we can fetch the JSON attribute name (country code), and by invoking the getValue() method we can fetch the value of the JSON attribute. The method getValue() returns a reference of the class JsonElement. As depicted in the presentation (page 16), class JsonElement is a parent class of  JsonPrimitive (representing a literal, such as a number, string, boolean, etc.), JsonObject, JsonArray and JsonNull. In our case, since we know the value of each attribute of the countriesJson is a JSON object, we cast it to JsonObject class.