Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with JSON streams efficiently (in Java)

Tags:

java

json

I've been using both JSONObject and JSONReader, but ideally I'm looking for a hybrid :)

In particular, given a stream of JSON objects, part of arbitrarily long JSON array, is there a helper/library that yields "JSONObject" at a time, iterator style, instead of reading everything in or having to parse out individual primitive fields (JsonReader)?

Example of hypothetical API:

JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));

reader.beginArray();
while (reader.hasNext()) 
{
  JSONObject obj = reader.readObject();
  // do something with 'obj'
}
reader.endArray();

Above, calling readObject parses some complex JSON sub-tree and returns it as JSONObject.

like image 325
Slawomir Avatar asked Jul 31 '14 16:07

Slawomir


People also ask

Can JSON be streamed?

Concatenated JSON streaming allows the sender to simply write each JSON object into the stream with no delimiters. It relies on the receiver using a parser that can recognize and emit each JSON object as the terminating character is parsed.

How do I get large JSON data from REST API?

The RESTful way of doing this is to create a paginated API. First, add query parameters to set page size, page number, and maximum number of items per page. Use sensible defaults if any of these are not provided or unrealistic values are provided. Second, modify the database query to retrieve only a subset of the data.

Does Java support JSON natively?

JSON is a lightweight text-based format that allows us to represent objects and transfer them across the web or store in the database. There is no native support for JSON manipulation in Java, however, there are multiple modules that provide this functionality.


1 Answers

There's the javax.json.JSONParser.

An example that prints out a JSON:

import javax.json.Json;
import javax.json.stream.JsonParser;
...
JsonParser parser = Json.createParser(new StringReader(jsonData));
while (parser.hasNext()) {
   JsonParser.Event event = parser.next();
   switch(event) {
      case START_ARRAY:
      case END_ARRAY:
      case START_OBJECT:
      case END_OBJECT:
      case VALUE_FALSE:
      case VALUE_NULL:
      case VALUE_TRUE:
         System.out.println(event.toString());
         break;
      case KEY_NAME:
         System.out.print(event.toString() + " " +
                          parser.getString() + " - ");
         break;
      case VALUE_STRING:
      case VALUE_NUMBER:
         System.out.println(event.toString() + " " +
                            parser.getString());
         break;
   }
}

from The Java EE Tutorial 19.4.1 Reading JSON Data Using a Parser. It's part of the JavaEE 7 API but a standalone jar can be gotten from https://jsonp.java.net/download.html.

like image 148
vandale Avatar answered Sep 28 '22 18:09

vandale