Java JSON Tutorial - JSON Java








JSR 353 addresses the standard Java API for JSON processing and shipped as part of JEE 7.

The reference implementation jsonp can be downloaded from https://jsonp.java.net/download.html.

To compile an application with JSON Processing API, declare a dependency on json-api in maven project.

The api dependency doesnt allow you to run the application.

<dependency>
    <groupId>javax.json</groupId>
    <artifactId>javax.json-api</artifactId>
    <version>1.0</version>
</dependency>

For non-maven projects, the JSON Processing API jar (http://search.maven.org/remotecontent?filepath=javax/json/javax.json-api/1.0/javax.json-api-1.0.jar) can be downloaded and included in the classpath for compiling the applications.

To run an application with JSON Processing API, declare a dependency on json-api implementation in your maven project.

<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.json</artifactId>
    <version>1.0.4</version>
</dependency>

For non-maven projects, the JSON Processing RI jar can be downloaded and included in the classpath for running the applications via http://search.maven.org/remotecontent?filepath=org/glassfish/javax.json/1.0.4/javax.json-1.0.4.jar.

GlassFish 4.0 already includes JSON implementation. For an web application, we don't need to include implementation jar in a war.

Declare an json-api dependency with provided scope during the development of the application.

<dependency>
    <groupId>javax.json</groupId>
    <artifactId>javax.json-api</artifactId>
    <scope>provided</scope>
    <version>1.0</version>
</dependency>




Example 1

The following code shows how to read JSON data to JSON Object Model using JsonReader.

import java.io.StringReader;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonValue;
 
public class Main {
    public static void main(String[] args) {
        String personJSONData = 
            "  {" +
            "   \"name\": \"Jack\", " +
            "   \"age\" : 13, " +
            "   \"isMarried\" : false, " +
            "   \"address\": { " +
            "     \"street\": \"#1234, Main Street\", " +
            "     \"zipCode\": \"123456\" " +
            "   }, " +
            "   \"phoneNumbers\": [\"011-111-1111\", \"11-111-1111\"] " +
            " }";
         
        JsonReader reader = Json.createReader(new StringReader(personJSONData));
         
        JsonObject personObject = reader.readObject();
         
        reader.close();
         
        System.out.println("Name   : " + personObject.getString("name"));
        System.out.println("Age    : " + personObject.getInt("age"));
        System.out.println("Married: " + personObject.getBoolean("isMarried"));

        JsonObject addressObject = personObject.getJsonObject("address");
        System.out.println("Address: ");
        System.out.println(addressObject.getString("street"));
        System.out.println(addressObject.getString("zipCode"));
         
        System.out.println("Phone  : ");
         JsonArray phoneNumbersArray = personObject.getJsonArray("phoneNumbers");
        for (JsonValue jsonValue : phoneNumbersArray) {
            System.out.println(jsonValue.toString());
        }
    }
}




Read Array

JsonReader reader = Json.createReader(new StringReader(personJSONData));
JsonArray personArray = reader.readArray();
reader.close();


for (JsonValue jsonVal : personArray) {
    System.out.println(personObj.getValueType() + " - " 
    + ((JsonObject) personObj).getString("name"));
}

Example 2

The following code shows how to building JSON Object using JsonObjectBuilder and JsonArrayBuilder

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
 
public class Main {
     
    public static void main(String[] args) {
        JsonObject personObject = Json.createObjectBuilder()
                .add("name", "John")
                .add("age", 13)
                .add("isMarried", false)
                .add("address", 
                     Json.createObjectBuilder().add("street", "Main Street")
                                               .add("city", "New York")
                                               .add("zipCode", "11111")
                                               .build()
                    )
                .add("phoneNumber", 
                     Json.createArrayBuilder().add("00-000-0000")
                                              .add("11-111-1111")
                                              .add("11-111-1112")
                                              .build()
                    )
                .build();
         
        System.out.println("Object: " + personObject);
    }
}

Example 3

The following code shows how to serialize JSON Object with JsonWriter.

import java.io.StringWriter;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonWriter;
 
public class Main {
     
    public static void main(String[] args) {
        JsonObject personObject = Json.createObjectBuilder()
                .add("name", "Jack")
                .add("age", 13)
                .add("isMarried", false)
                .add("address", 
                     Json.createObjectBuilder().add("street", "Main Street")
                                               .add("city", "New York")
                                               .add("zipCode", "11111")
                                               .build()
                    )
                .add("phoneNumber", 
                     Json.createArrayBuilder().add("00-000-0000")
                                              .add("11-111-1111")
                                              .add("11-111-1112")
                                              .build()
                    )
                .build();
         
        StringWriter stringWriter = new StringWriter();
        JsonWriter writer = Json.createWriter(stringWriter);
        writer.writeObject(personObject);
        writer.close();
        System.out.println(stringWriter.getBuffer().toString());
    }
 
}