Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Getting to Know JSON-P 1.1 (Part 1)

DZone's Guide to

Getting to Know JSON-P 1.1 (Part 1)

Want to see what Java EE 8 added to the JSON-P API? This quick guide will walk you through some simple methods and their uses.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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 you have 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 to 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, use the JSON pointer expression of the element to replace. Then, 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 existence of a value at a location can be tested with the containsValue() method. The code snippet below tests to see if there is a value at the location expressed by the JSON Pointer expression /doesNotExist.

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


Related Articles

Top 5 new features in Java EE 8
Java EE 8: Only What’s New – First Look
Jakarta EE: 5 Things You Need to Know
Java EE 8: Only What’s New
Java EE 8 Security API Overview


Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:
java ,java ee ,json-p 1.1 ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}