• Home
KodEduKodEdu
  • Home

Java API for JSON Processing – Creating JSON Objects

  • Posted by Rahman Usta
  • Categories Genel, Uncategorized, Yazılar
  • Date 15 June 2013

json_logo 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.

 

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

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 =
        Json.createObjectBuilder()
                .add("name", "Fikret")
                .add("surname", "Savaş")
                .add("age", 25)
        .build();

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

[
  "A",
  5,
  6.3,
  false,
  30,
  {
    "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
        .build();

Example 3: A Json object which hosts Json array JSON Format

{
  "name": [
    "Emin",
    "Şahin"
  ],
  "surname": "Demir"
}

JsonObject Representation

JsonArray jsonArray2=Json.createArrayBuilder()
        .add("Emin").add("Şahin").build();
JsonObject jsonObject2 = Json.createObjectBuilder()
        .add("name", jsonArray2)
        .add("surname", "Demir")
        .build();

Example 4: A complex Json object JSON Format

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

JsonObject Representation

JsonObject jsonObject3 =
    Json.createObjectBuilder()
        .add("name", "Ersin")
        .add("surname", "Çetinkaya")
        .add("age", 25)
        .add("address",
              Json.createObjectBuilder()
                  .add("city", "Bursa")
                  .add("country", "Türkiye")
                  .add("zipCode", "33444"))
        .add("phones",
                      Json.createArrayBuilder()
                          .add("234234242")
                          .add("345345354"))
        .build();

You can access to the examples from here.

Hope to see you again.

Tag:backend, json, json-p

  • Share:
author avatar
Rahman Usta
Java Champion, Java* Trainer and Author. JCP Expert, Istanbul JUG and Adopt a JSR member. Duke's Choice Award 2015 winner. Ex-Voxxed Istanbul Organizer. Creator of AsciidocFX.

Previous post

CDI – @Default and @Inject Annotations
15 June 2013

Next post

How to Remove Unnecessary Jars!
19 June 2013

You may also like

collector
Distributed Map-Reduce Model using Java 8 and Java EE 7 WebSocket
16 November, 2014
server-sent-events-jax-rs-2
HTML 5 Server Sent Events on Glassfish 4
27 November, 2013
concurrency
Java EE 7 – Concurrency Utilities
23 October, 2013

Leave A Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • TürkçeTürkçe

Gözde yazılar

Combine JaxRS 2.0 Rest Client, Java FX 2, Spring MVC and Hibernate
15Aug2013
Java EE 7 – Concurrency Utilities
23Oct2013
HTML 5 Server Sent Events on Glassfish 4
27Nov2013
Distributed Map-Reduce Model using Java 8 and Java EE 7 WebSocket
16Nov2014

Recent Posts

  • First look at HTTP/2 Server Push in Java Servlet 4.0 Specification 26 April 2017
  • Distributed Map-Reduce Model using Java 8 and Java EE 7 WebSocket 16 November 2014
  • HTML 5 Server Sent Events on Glassfish 4 27 November 2013
  • Java EE 7 – Concurrency Utilities 23 October 2013
  • Combine JaxRS 2.0 Rest Client, Java FX 2, Spring MVC and Hibernate 15 August 2013

Get Java Software

React.js Eğitimi Başlıyor
11-22 Eylül, 2017
Eğitmen
Rahman Usta
İletişim

merhaba@kodedu.com

  • Hakkında
  • Gizlilik Politikası
  • İletişim
  • Referanslar
Kodedu Bilişim Danışmanlık
Cemil Meriç mah. Çelebi sok.
No:16/3 Ümraniye/İSTANBUL
Tel: 0850 885 38 65
Alemdağ V.D.: 8960484815

KODEDU © Tüm hakları saklıdır.