News Ticker

Get to Know JSON Pointer: JSON-P 1.1 Overview Series

Get to Know JSON Pointer JSON-P 1.1 Overview Series

Java EE 8 includes an update to the JSON Processing API and brings it up to date with the latest IEFT standards for JSON. They are:

I will cover these topics in this mini-series.

Getting Started

To get started with JSON-P you will need the following dependencies from the Maven central repository.

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

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

JSON-Pointer

A JSON Pointer defines a string expression that references an element within the hierarchical structure of a JSON document. With a JSON pointer expression, you can access and manipulate a JSON document by retrieving, adding, removing and replacing an element or value referenced by the expression.

The entry API is the javax.json.JsonPointer interface. An instance is created by calling the static factory method createPointer(String expression) on the javax.json.Json class and passing it the pointer expression.

Retrieve a Value

If given the JSON document below and you want to retrieve the value of the title element you create the JSON Pointer expression /title.

{
"title": "Java EE: Only What's New",
"author": "Alex Theedom",
"chapters": [
  "Chapter 1: Java EE 8 What’s New Overview",
  "Chapter 2: Java API for JSON Binding 1.0 (JSR 367)",
  "Chapter 3: Java EE Security API 1.0 (JSR 375)"
],
"released": true,
"pages": 300,
"sourceCode": {
  "repositoryName": "Java-EE-8-Only-Whats-New",
  "url": "github.com/readlearncode/"
},
"otherBooks": [
  {
    "title": "Professional Java EE Design Patterns",
    "length": 350
  }
]
}

JsonObject jsonObject = ... create JSONObject from JSON document ...;

The code snippet below creates a JsonPointer and references the title element. It then calls the getValue() method which is passed the JsonObject to query.

JsonValue jsonValue = Json.createPointer("/title").getValue(jsonObject);

Add a Value

To add (or insert) a value to a JSON document follow the same logic as retrieval by using a JSON pointer expression to identify the insertion point within the document. The following code snippet adds a new “category”: “Programming” JSON object to the root of the document.

JsonObject jsonObject = Json
      .createPointer("/category")
      .add(jsonObject, Json.createValue("Programming"));

The JsonObject returned is the entire new modified object.

Remove a Value

The removal process requires the location of the value to remove expressed as a JSON Pointer expression. The code snippet below removes the title element and returns the modified JSON document as a JsonStructure instance

JsonStructure jsonStructure = Json.createPointer("/title").remove(jsonObject);

Replace a Value

To replace a value the JSON pointer expression of the element to replace and the replacement element is passed to the replace() method. The code snippet below replaces the title element’s value and returns the modified JSON document.

JsonStructure jsonStructure = Json
     .createPointer("/title")
     .replace(jsonObject, Json.createValue("Java EE 8"));

Test a Value

The exists of a value at a location can be tested with the containsValue() method. The code snippet below tests to see it there is a value at the location expressed by the JSON Pointer expression /doesNotExist.

Boolean containsValue = Json
     .createPointer("/doesNotExist")
     .containsValue(jsonObject);

Conclusion

Well, that’s it for the first article in this mini-series about JSON Processing’s new features. If you want to know more about JSON Processing and Java EE then my new book Java EE 8: Only What’s New is the book for you, and it’s available for a special launch price of only $9.95.

Follow this link to get your copy, today.

That’s all for now.

Related Articles

4 Trackbacks / Pingbacks

  1. Get to Know JSON Patch: JSON-P 1.1 Overview Series
  2. Get to Know JSON Merge Patch: JSON-P 1.1 Overview Series
  3. Java EE 8 Security API Overview
  4. Get to Know JSON Binding: Overview Series

Leave a Reply

%d bloggers like this: