Over a million developers have joined DZone.
Platinum Partner

Java API for JSON Processing – Creating JSON Objects

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.

Java API for JSON Processing (JSON-P) is a Java Standard defined with the JSR-353, which  is a part of Java EE 7. It is called JSON-P as abbreviation but it does not seem appropriate because of causing confusion with JSON Padding. For this reason, it may cause confusion at first glance but even so, I would prefer JSON-P abbreviation in this article.

The irreducible rise of the RESTful web services has provided JCP to give more proclivities to this process and make them enter into the process of establishing a standard for the manipulation of JSON in Java EE 7. People who deal with XML technologies are familiar with Stax and DOM technologies that are used on behalf of processing and managing XML. General libraries about Stax and DOM already exist for a long time, which are for XML data exchange format in Java SE. Now, Stax and DOM approaches used for XML together with JSON-P will be introduced in a manner similar to JSON.

Although there exists libraries on the basis of Stax and DOM in JSON-P, there were not any APIs which undertake the conversion of Object<>JSON as similar to JAXB. If you develope Restful Webservices with Jax-RS before, software exist in Java ecosystem such as Jackson, Jettison was already being used for Json Binding operations. These operations in JAX-RS was being resolved by converting the object into XML format previously and then converting the converted XML data in JSON format. But with Jax-RS 2 and Json-P, container can convert Json based Java objects to Json representation and vice verse.

As you can access to the JSON-P project from the http://json-processing-spec.java.net/address, you can also use dependency where libraries exist through the following Maven dependency component.


Now I want to give you examples about how to build JSON objects with JSON-P.
Whereas a simple JSON object accepts data in the { [ key ]:[ value ] , .. } format,
A simple JSON array accepts data in the [ val1,val2, .. , val3 ] format.

Example 1: A simple JSON object

JSON Format

  "name": "Fikret",
  "surname": "Savaş",
  "age": 25

JsonObject Representation

JsonObject jsonObject1 =
                .add("name", "Fikret")
                .add("surname", "Savaş")
                .add("age", 25)

Example 2: A complex array of Json which hosts Json object
JsonArray object in JSON-P can accept such components in the type of String, int, double, boolean, BigInteger, BigDecimal. In addition, it also accepts objects in the type of JsonValue interface (such as JsonObject, JsonArray).

JSON Format

    "name": "Fikret",
    "surname": "Savaş",
    "age": 25

JsonArray Representation

JsonArray jsonArray1= Json.createArrayBuilder()
        .add("A") // String
        .add(5)   // int
        .add(6.3) // double
        .add(false)  // boolean
        .add(new BigInteger("30"))  // Big(Integer) | (Decimal)
        .add(jsonObject1)  // JsonValue

Example 3: A Json object which hosts Json array

JSON Format

  "name": [
  "surname": "Demir"

JsonObject Representation

JsonArray jsonArray2=Json.createArrayBuilder()
JsonObject jsonObject2 = Json.createObjectBuilder()
        .add("name", jsonArray2)
        .add("surname", "Demir")

Example 4: A complex Json object

JSON Format

  "name": "Ersin",
  "surname": "Çetinkaya",
  "age": 25,
  "address": {
    "city": "Bursa",
    "country": "Türkiye",
    "zipCode": "33444"
  "phones": [

JsonObject Representation

JsonObject jsonObject3 =
        .add("name", "Ersin")
        .add("surname", "Çetinkaya")
        .add("age", 25)
                  .add("city", "Bursa")
                  .add("country", "Türkiye")
                  .add("zipCode", "33444"))

The real content and sample codes can be access at http://en.kodcu.com/2013/06/java-api-for-json-processing-creating-json-objects/

Hope to see you again.

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}