JSON Pointer Example

This post explains JSON Pointer with an example.

JSON Pointer is a standard that defines a string syntax, which can used to access a particular field or key value in the entire json document.

For example in the below json document

json document


{
    "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" }
    ]
}

In JSON Pointer every string syntax is prefixed with “/”.

So to access the value of firstName, the syntax will be
/firstName

to access the value of phoneNumbers, the syntax will be
/phoneNumbers

to access the first value in phoneNumbers array, the syntax will be
/phoneNumbers/0

to access the whole json structure, the syntax will be just forward slash
/

The Java JSON 1.1 api provides JSON Pointer features, where we can test the above syntax, as shown below

Main Code


1  import java.io.FileReader;
2  
3  import javax.json.Json;
4  import javax.json.JsonArray;
5  import javax.json.JsonObject;
6  import javax.json.JsonPointer;
7  import javax.json.JsonReader;
8  import javax.json.JsonString;
9  import javax.json.JsonStructure;
10 import javax.json.JsonValue;
11 
12 public class Example3 {
13  public static void main(String[] args) throws Exception {
14      JsonReader reader = Json.createReader(new FileReader("jsondata.txt"));
15      JsonStructure jsonStructure = reader.read();
16      
17      JsonPointer jsonPointer1 = Json.createPointer("/firstName");
18      JsonString jsonString = (JsonString) jsonPointer1.getValue(jsonStructure);
19      System.out.println(jsonString.getString());
20      
21      JsonPointer jsonPointer2 = Json.createPointer("/phoneNumbers");
22      JsonArray array = (JsonArray) jsonPointer2.getValue(jsonStructure);
23      for(JsonValue value : array) {
24          JsonObject objValue = (JsonObject)value;
25          System.out.println(objValue.toString());
26      }
27      
28      JsonPointer jsonPointer3 = Json.createPointer("/phoneNumbers/0");
29      JsonObject jsonObject1 = (JsonObject)jsonPointer3.getValue(jsonStructure);
30      System.out.println(jsonObject1.toString());
31      
32      JsonPointer jsonPointer4 = Json.createPointer("");
33      JsonObject jsonObject2 = (JsonObject)jsonPointer4.getValue(jsonStructure);
34      System.out.println(jsonObject2.toString());
35      
36      reader.close();
37  }
38 }

Explanation

At line 17, 21, 28, and 32 we create different instances of JsonPointer, which represents the string syntax

The we get the value of the key referred by the string syntax, by calling getValue on JsonPointer object and pass the json document, in which the mentioned key
has to be searched.

Refer to line 18, 22, 29, and 33.

At line 19, 23, 30, and 34, we print the values.

Output

Duke
{“Mobile”:”111-111-1111″}
{“Home”:”222-222-2222″}
{“Mobile”:”111-111-1111″}
{“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″}]}

Leave a Reply