DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Leveraging Natural Language Processing for Enhancing Sales Chatbots
  • Streamlining Event Data in Event-Driven Ansible
  • Dynamic Web Forms In React For Enterprise Platforms
  • Unlocking Oracle 23 AI's JSON Relational Duality

Trending

  • Web Crawling for RAG With Crawl4AI
  • Prioritizing Cloud Security Risks: A Developer's Guide to Tackling Security Debt
  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  • Operational Principles, Architecture, Benefits, and Limitations of Artificial Intelligence Large Language Models
  1. DZone
  2. Coding
  3. Languages
  4. JSON Processing 1.1: The JSON Pointer Challenge

JSON Processing 1.1: The JSON Pointer Challenge

Want to see what the new JSON Pointer, straight from the JSON-P 1.1 API, is capable of? Here's a challenge and a corresponding explanation.

By 
Alex Theedom user avatar
Alex Theedom
·
Updated Nov. 27, 17 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
19.0K Views

Join the DZone community and get the full member experience.

Join For Free

Given the JSON document in the code snippet below, what is the result of using the JSON Pointer feature from JSON-P 1.1?

JSON_pointer_example

The new shape of the JSON object will be as follows:

{
    "name": "Duke",
    "likes": [
        "Java EE 8",
        "Java",
        "Ice Cream"
    ]
}


What Is JSON Pointer?

JSON Pointer is a new feature you will find in the JSON Processing 1.1 API and brings it up to date with the latest IEFT standards JSON Pointer. This updated API forms part of the Java EE 8 release.

So what is a JSON Pointer? Well, a JSON Pointer defines a string expression that identifies a specific value within a JSON document. It is akin to XPointer, which is used to identify fragments within an XML document.

JSON Pointer in Action

Let’s take a look at an example. Given the JSON document below, the first element in the likes array would be referred to via the JSON pointer expression: /likes/0.

{
    "name": "Duke",
    "likes": [
        "Java",
        "Coffee"
    ]
}


This refers the element value: Java.

How to Use JSON Pointer

The entry API is the JsonPointer interface. An instance is created by calling the static factory method createPointer() on the Json class. The code snippet below creates a JsonPointer and references the second element in the likes array:

JsonPointer pointer = Json.createPointer("/likes/0");


The JsonPointer API can also mutate the JSON document by adding, replacing, and removing properties. The code snippet below adds the value “Java EE 8” to the likes list:

pointer.add(jsonObject, Json.createValue("Java EE 8"));


And the code in the following snippet replaces the value at the 3rd index position:

pointer = Json.createPointer("/likes/2");
JsonObject newJsonObject = pointer.replace(jsonObject, Json.createValue("Ice Cream"));


Putting it all together, you have code that adds an element of the likes array and the replaces an element. The full code snippet looks like this:

JsonObject jsonObject = 
    Json.createReader(new StringReader(target)).readObject();
JsonPointer pointer = Json.createPointer("/likes/0");
jsonObject = pointer.add(jsonObject, Json.createValue("Java EE 8"));
pointer = Json.createPointer("/likes/2");
JsonObject newJsonObject = 
    pointer.replace(jsonObject, Json.createValue("Ice Cream"));


The output from this code is:

{
    "name": "Duke",
    "likes": [
        "Java EE 8",
        "Java",
        "Ice Cream"
    ]
}


Further Reading

For further information on how to use JSON Processing’s new features please take a look at my article What’s new in Java EE 8 over at IBM developerWorks and also you should read the specifications for the Java API for JSON Processing 1.1 (JSR 374).

GitHub Repository

The code from this and all other #100DaysOfJavaEE8 can be found in my GitHub repository.

JSON Pointer (computer programming) Processing

Published at DZone with permission of Alex Theedom, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Leveraging Natural Language Processing for Enhancing Sales Chatbots
  • Streamlining Event Data in Event-Driven Ansible
  • Dynamic Web Forms In React For Enterprise Platforms
  • Unlocking Oracle 23 AI's JSON Relational Duality

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!