The Java API for JSON Processing (JSON-P) enables Java EE applications to parse, transform, and query JSON data using either the Object model or the Streaming model.
JSON is a text-based data exchange format derived from JavaScript that is used in Web Services and other connected applications.
JSON-P is introduced in the Java EE 7 platform.
There are two programming models used for generating and parsing JSON data. Object model and Streaming model.
The Object model creates a tree structure in memory that represents the JSON data. The tree can then be navigated, analyzed, or modified.
The Streaming model uses an event-based parser that reads JSON data one element at a time. Each element can be processed or discarded by the application code, and then the parser proceeds to the next event.
The parser generates events and stops for processing when an object or an array begins or ends, when it finds a key, or when it finds a value.
The JSON-P API provides the following key packages to generate and parse JSON data.
javax.json - This package contains interfaces and classes for generation and parsing JSON data using Object model.
javax.json.stream - This package contains interfaces and classes for generating and parsing JSON data using Streaming model.
javax.json package contains the following key classes and interfaces to generate and parse JSON objects using the object model.
Json - Contains static methods to create instances of JSON parsers, builders and generators.
JsonReader - Reads JSON data as stream and create an object model in memory.
JsonObjectBuilder - Creates object model in memory by adding elements through application code.
JsonWriter - Writes an object model from memory to a stream.
javax.json.stream package contains the following key classes and interfaces to generate and parse JSON objects using the stream model.
JsonParser - Event based parser that can read JSON data from a stream.
JsonGenerator - Generates JSON data to a stream, one element at a time.
You can create an object model using JSON data using following steps
1. Create an instance of JsonReader class from the Json utility class and pass the JSON data file as a parameter.
2. Call read() method on the reader instance which returns the JSON object model.
JsonReader reader = Json.createReader(new FileReader('jsondata.txt'));
JsonStructure jsons = reader.read();
You can create a JSON object model using application code using following steps
1. Create an instance of JsonObject class using the Json utility class.
Add key value pairs, objects or arrays to the instance of JsonObject.
JsonObject modelObject = Json.createObjectBuilder();
modelObject.add('firstName', 'Java');
modelObject.add('lastName', 'Guru');
modelObject.add('city', 'San Francisco');
modelObject.build();
You can output a JSON object model to a stream by following steps.
1. Create an instance of JsonWriter by using the Json utility class.
2. Call the method writeObject() on the instance of JsonWriter and pass the instance of JSON model as a parameter.
StringWriter writer = new StringWriter();
JsonWriter jsonWriter = Json.createWriter(writer);
jsonWriter.writeObject(model);
jsonWriter.close();
//print the json data
String jsonData = stWriter.toString();
System.out.println(jsonData);