Operations that can be performed using JSON Pointer

This post explains the operations that can be performance on json data with the help of new JSONP 1.1 api.

As we know from our previuos post JSON Pointer is a string syntax used to identify and locate a particular field or property in a json file.

Once identified and located we can perform the following opertions
1) Add a new field
2) Remove an existing field
3) Replace an existing field
4) Retrieve the value of an existing field
5) Check the existence of field or a field’s value.

Below is the example

Input json file: jsondata5.txt

    "firstName": "Duke",
    "lastName": "Java",
    "age": 18,
    "streetAddress": "100 Internet Dr",
    "city": "JavaTown",
    "state": "JA",
    "postalCode": "12345",
    "phoneNumbers": [
        { "Mobile": "111-111-1111" },
        { "Home": "222-222-2222" }

Main Code

1  import java.io.FileReader;
2  import java.io.FileWriter;
4  import javax.json.Json;
5  import javax.json.JsonNumber;
6  import javax.json.JsonPointer;
7  import javax.json.JsonReader;
8  import javax.json.JsonString;
9  import javax.json.JsonStructure;
10 import javax.json.JsonWriter;
12 public class Example5 {
13  public static void main(String[] args) throws Exception {
14      JsonReader reader = Json.createReader(new FileReader("jsondata5.txt"));
15      JsonStructure jsonStructure = reader.read();
16      reader.close();
18      //Replace a field value
19      JsonPointer jsonPointer1 = Json.createPointer("/firstName");
20      JsonString jsonString1 = (JsonString) jsonPointer1.getValue(jsonStructure);
21      System.out.println(jsonString1.getString());
22      jsonString1 = Json.createValue("lava");
23      jsonStructure = jsonPointer1.replace(jsonStructure, jsonString1);
25      System.out.println(jsonStructure);
27      //Remove a field
28      JsonPointer jsonPointer2 = Json.createPointer("/age");
29      JsonNumber jsonNumber1 = (JsonNumber) jsonPointer2.getValue(jsonStructure);
30      System.out.println(jsonNumber1.intValue());
31      jsonStructure = jsonPointer2.remove(jsonStructure);
32      System.out.println(jsonStructure);
34      //Check the existence of property or properties value
35      boolean found = jsonPointer2.containsValue(jsonStructure);
36      System.out.println(found);
38      //Add a new field
39      JsonPointer jsonPointer3 = Json.createPointer("/sex");
40      JsonString jsonString2 = Json.createValue("M");
41      jsonStructure = jsonPointer3.add(jsonStructure, jsonString2);
43      System.out.println(jsonStructure);
45      JsonWriter writer = Json.createWriter(new FileWriter("jsondata6.txt"));
46      writer.write(jsonStructure);
47      writer.close();
48  }
49 }


At line 15, we read the json content from the file.

From line 19 to 23, we replace the value of field “firstname” from “Duke” to “lava” by calling replace method which takes the entire json structure and the
new vale

From line 28 to 31, we remove an existing “age” field by calling remove method. The remove method takes existing jsonStructure from which it has to remove
the field and return a new jsonStructure which will not have the age field.

At line 35 we check whether the new json structure has the field age, by calling the method containsValue. This method return true if value or field is present
else false.

From line 39 to 41, we add a new field named “sex” at the end of the new json structure.
First at line 39 we create a json pointer, by calling createPointer, the parameter passed will be the name of the new field.
Next at line 40, we create the value
At line 41, we add to the new json structure by calling the add method.

From line 45 to 47, we write the new json structure to the new file.


{"firstName":"lava","lastName":"Java","streetAddress":"100 Internet Dr","city":"JavaTown","state":"JA","postalCode":"12345","phoneNumbers":[{"Mobile":"111-111-1111"},{"Home":"222-222-2222"}],"sex":"M"}

Leave a Reply