In this article I am going to explain how to read JSON with JAVA using Google-json library. The basic part of the JSON is JsonElement. There are 4 types JSON elements.
  1. Json Object : Surrounded with curly braces and have child JSON elements
  2. Json Primitive Element : It contains field name and it's value
  3. Json Array Element : It contains field name and array of JSON elements.
  4. Json Null Element : It contains nothing 

Program Flow

Program flow for JSON reading is shown in the below diagram. 
  1. If element is JSON Object then iterate it into field names and JSON elements and process those JSON elements. 
  2. If element is JSON Array then iterate it into JSON elements and process those JSON elements.
  3. If element is JSON Primitive then print its value as String

Java Code :

In the below example there are 3 JSON input strings, One is normal JSON String, Second-one is JSON Array, Third-one is Null.
import java.util.Map.Entry;
import java.util.Set;

public class JSONExample {
    public static void main(String[] args) {
        JsonParser parser = new JsonParser();
        // JSON String
        String json = "{" + "\"age\":100," 
                + "\"name\":\"srinivas\","
                + "\"school\":\"\","
                + "\"messages\":[\"msg 1\",\"msg 2\",\"msg 3\"]}";
        // JSON Array
        String jsonArray = "[\"msg 1\",\"msg 2\",\"msg 3\"]";
        // JSON NULL
        String jsonNull = "";
        Object jsonElement = parser.parse(new StringReader(json));
        Object jsonEleArray = parser.parse(new StringReader(jsonArray));
        Object jsonEleNull = parser.parse(new StringReader(jsonNull));
        printJson((JsonElement) jsonElement);
        printJson((JsonElement) jsonEleArray);
        printJson((JsonElement) jsonEleNull);

    public static void printJson(JsonElement jsonElement) {
        // Check whether jsonElement is JsonObject or not
        if (jsonElement.isJsonObject()) {
            Set<Entry<String, JsonElement>> ens = ((JsonObject) jsonElement).entrySet();
            if (ens != null) {
                // Iterate JSON Elements with Key values
                for (Entry<String, JsonElement> en : ens) {
                    System.out.println(en.getKey() + " : ");
        // Check whether jsonElement is Arrary or not
        else if (jsonElement.isJsonArray()) {
                    JsonArray jarr = jsonElement.getAsJsonArray();
                    // Iterate JSON Array to JSON Elements
                    for (JsonElement je : jarr) {
        // Check whether jsonElement is NULL or not
        else if (jsonElement.isJsonNull()) {
            // print null
        // Check whether jsonElement is Primitive or not
        else if (jsonElement.isJsonPrimitive()) {
            // print value as String


  1. do not just write the concept of json.Demonstrate the live implementation of autcomplete in facebook,mail id auto completion in gmail.Also include where else a begineer can directly use this concept

  2. Very good explanation with code example.

  3. This comment has been removed by the author.

  4. This comment has been removed by the author.



Follow this blog by Email

Popular Posts