Tuesday 30 March 2021

Javax.json: Query json using streaming api

 

The Streaming API of JSON-P supports parsing a JSON string. Streaming API is more effective and preferrable method to parse big json documents. Streaming API provides JsonParser class which provides extremely fast, read-only, forward access to json documents.

 

JsonParser.Event enum provides following constants to identify the events in parsing state.

 

Constant

Description

START_ARRAY

Represent the start of a json array

START_OBJECT

Start of a JSON object. The position of the parser is after '{'.

KEY_NAME

Name in a name/value pair of a JSON object. The position of the parser is after the key name. The method getString returns the key name.

VALUE_STRING

String value in a JSON array or object. The position of the parser is after the string value. The method getString returns the string value.

VALUE_NUMBER

Number value in a JSON array or object. The position of the parser is after the number value. JsonParser provides the following methods to access the number value: getInt, getLong, and getBigDecimal.

VALUE_TRUE

true value in a JSON array or object. The position of the parser is after the true value.

VALUE_FALSE

false value in a JSON array or object. The position of the parser is after the false value.

VALUE_NULL

null value in a JSON array or object. The position of the parser is after the null value.

END_OBJECT

End of a JSON object. The position of the parser is after '}'.

END_ARRAY

End of a JSON array. The position of the parser is after ']'.

 

How to create a parser from json string?

'Json.createParser' method is used to create a parser.

 

Example

JsonParser parser = Json.createParser(new StringReader(jsonString));

 

Once you got the JsonParser instance, you can use hasNext() and next() methods to parse the json string.

 

Example

while(parser.hasNext()){
   final Event event = parser.next();
   switch(event){
      ......
   }
}

 

Find the below working application.

 

JsonParserDemo.java

package com.sample.app;

import java.io.StringReader;
import java.math.BigDecimal;

import javax.json.Json;
import javax.json.stream.JsonParser;
import javax.json.stream.JsonParser.Event;

public class JsonParserDemo {

        public static void main(String[] args) {

                String jsonString = "{\n" + "  \"id\": 1,\n" + "  \"firstName\": \"Ram\",\n" + "  \"lastName\": \"Gurram\"\n"
                                + "}";

                JsonParser parser = Json.createParser(new StringReader(jsonString));
                String key = null;
                while (parser.hasNext()) {
                        final Event event = parser.next();
                        switch (event) {
                        case KEY_NAME:
                                key = parser.getString();
                                System.out.print(key);
                                break;
                        case VALUE_STRING:
                                String string = parser.getString();
                                System.out.println(" -> " + string);
                                break;
                        case VALUE_NUMBER:
                                BigDecimal number = parser.getBigDecimal();
                                System.out.println(" -> " + number);
                                break;
                        case VALUE_TRUE:
                                System.out.print(true);
                                break;
                        case VALUE_FALSE:
                                System.out.print(false);
                                break;
                        case START_ARRAY:
                                System.out.println("[");
                                break;
                        case END_ARRAY:
                                System.out.println("]");
                                break;
                        case START_OBJECT:
                                break;
                        case VALUE_NULL:
                                System.out.print("null");
                                break;
                        case END_OBJECT:
                                System.out.println();
                                break;
                        default:
                                break;
                        }
                }
                parser.close();
        }

}

 

Output

id -> 1
firstName -> Ram
lastName -> Gurram

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment