Contents

Quick guide on Jsonp from Jakarta

Written by: David Vlijmincx

Introduction

JsonP is a library that is part of Jakarta and helps you create and read Json. It's a bit more low-level than JsonB but sometimes that is what you need.

Creating a JSON object

Using the Json class you create objects with the createObjectBuilder() method. Using the add it's possible to add properties to your JSON object. In the following example, you can see how to use this class. In the example an object is created that consists of properties, sub-objects, and arrays.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
 JsonObject person = Json.createObjectBuilder()
                .add("name", "John Doe")
                .add("age", 30)
                .add("Pet", Json.createObjectBuilder()
                        .add("name", "Rex")
                        .add("type", "Dog")
                        .add("age", 20)
                )
                .add("city", "New York")
                .add("isEmployee", true)
                .add("hobbies", Json.createArrayBuilder()
                        .add("reading")
                        .add("hiking")
                        .add("photography"))
                .build();

The resulting JSON looks like this.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
{
   "name":"John Doe",
   "age":30,
   "Pet":{
      "name":"Rex",
      "type":"Dog",
      "age":20
   },
   "city":"New York",
   "isEmployee":true,
   "hobbies":[
      "reading",
      "hiking",
      "photography"
   ]
}

Parsing JSON

It's also possible to parse JSON using JsonP. In the following example, a JSON value is parsed to a JsonObject that can be used to access its values.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// Parse JSON from String
String jsonString = "{\"name\":\"John Doe\",\"age\":30}";

try (JsonReader jsonReader = Json.createReader(new StringReader(jsonString))) {
    JsonObject object = jsonReader.readObject();

    // Access values
    String name = object.getString("name");
    int age = object.getInt("age");
    
    System.out.println("name = " + name);
    System.out.println("age = " + age);

}

The resulting object can be used to access the values.

Creating a JSON array

When you have a jsonArray it is possible to use it inside a loop. In the following example, a for-each loop prints each of the values inside the array.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Create JSON array
JsonArray colors = Json.createArrayBuilder()
        .add("red")
        .add("green")
        .add("blue")
        .build();

// Read array
for (JsonValue value : colors) {
    if (value instanceof JsonString stringValue) {
        System.out.println(stringValue.getString());
    }
}

JSON Pointer

A pointer points to a place inside a JSON. This doesn't have to be the same type. A pointer looks for a value inside the object. In the following example, there are two jsons and a single pointer that points to a “city” property.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
JsonObject usa = Json.createObjectBuilder()
        .add("city", "New York")
        .add("country", "USA")
        .build();

JsonObject netherlands = Json.createObjectBuilder()
        .add("city", "Amsterdam")
        .add("country", "Netherlands")
        .build();

JsonPointer pointer = Json.createPointer("/city");
System.out.println(pointer.getValue(usa).toString());
System.out.println(pointer.getValue(netherlands).toString());

In the example, the pointer points first to the USA JOSN followed by the Netherlands pointer.

Dependencies

If you are working on a project that uses Jakarta you probably have this dependency (indirectly) but if you are not using Jakarta you need to add these dependencies to use JsonP.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<dependency>
    <groupId>jakarta.json</groupId>
    <artifactId>jakarta.json-api</artifactId>
    <version>2.1.3</version>
</dependency>
<dependency>
    <groupId>org.eclipse.parsson</groupId>
    <artifactId>parsson</artifactId>
    <version>1.1.4</version>
    <scope>runtime</scope>
</dependency>