SEPTEMBER,
2024
JSON Processing in
Java
JUNE, 2024
CONTENT
Introduction to JSON
Java API for JSON Processing (JSR 353)
JSON Processing in the Java EE Platform
Object Model API
Streaming API
JSON in Java EE RESTful Web Services
Key Classes in JSON Processing
SEPTEMBER,
2024
Introduction to JSON
• JSON (JavaScript Object Notation) is a lightweight
data exchange format.
• Widely used in web services and connected
applications.
{
• Simple key-value pairs
"name": "John",
and array structures.
• Example: "age": 30,
"address": {"city": "New York", "zip":
"10001"}
}
SEPTEMBER,
2024
Java API for JSON
Processing (JSR 353)
• JSR 353 introduces a standard Java @Path("/user")
API for processing JSON. @Produces(MediaType.APPLICATION_J
SON)
• Supports two processing models:
public class UserResource {
Object Model and Streaming @GET
Model. public JsonObject getUser() {
• Facilitates parsing, transforming, return [Link]()
and querying JSON data. .add("name", "John")
.add("age", 30)
• Example:
.build();
}
}
SEPTEMBER,
2024
Object Model API
• Manipulates JSON data as objects
(tree structure). JsonObject jsonObject =
[Link]()
• Reads the entire JSON structure in
.add("name", "Alice")
memory. .add("age", 28)
• Ideal for smaller, simpler data .add("city", "Los Angeles")
manipulation. .build();
• Example:
// Accessing values
⚬ Demonstrates building and
String name =
accessing a JSON object. [Link]("name");
SEPTEMBER,
2024
Streaming API
JsonParser parser = [Link](new
StringReader(jsonData));
• Efficient for large datasets. while ([Link]()) {
• Reads and writes JSON as a stream [Link] event = [Link]();
switch (event) {
of tokens (pull parsing). case START_OBJECT:
[Link]("Start object");
• Lower memory footprint compared break;
to the Object Model. case KEY_NAME:
[Link]("Key: " +
• Example: [Link]());
⚬ Demonstrates streaming JSON break;
case VALUE_STRING:
parsing. [Link]("Value: " +
[Link]());
break;
// Handle other events like START_ARRAY,
END_ARRAY, etc.
SEPTEMBER,
2024
Comparison: Object
Model vs. Streaming API
• Object Model: Simple, higher memory use, full JSON in
memory.
• Streaming: Efficient, lower memory use, processes data
incrementally.
SEPTEMBER,
2024
JSON in Java EE RESTful
Web Services
• JSON is widely used in Java EE RESTful web
services.
• Easily integrated with JAX-RS for building APIs.
• Supports JSON-P and JSON-B for flexible
processing.
SEPTEMBER,
2024
The jsonp model Example
Application
• Example demonstrating the public class JsonpModelExample {
use of the Object Model API. public static void main(String[]
• Builds and manipulates JSON args) {
data for web services. JsonObject json =
[Link]()
.add("id", 101)
.add("name",
"Sample App")
.build();
[Link]([Link]());
SEPTEMBER,
2024
The jsonp streaming
Example Application
• Example demonstrating the public class JsonpStreamingExample {
public static void main(String[] args) {
Streaming API. String jsonData = "{\"id\":101,\"name\":\"Sample
• Efficiently processes large App\"}";
JsonParser parser = [Link](new
JSON data streams.
StringReader(jsonData));
while ([Link]()) {
[Link] event = [Link]();
if (event == [Link].KEY_NAME) {
[Link]([Link]());
}
}
}
}
SEPTEMBER,
2024
Key Classes in JSON
• JsonObject
Processing
⚬ Represents a JSON object
• JsonArray
⚬ Represents an array of
in memory (key-value JSON values.
pairs). ⚬ Used when working with
⚬ Allows manipulation of lists or arrays in JSON
JSON data as an object. data.
⚬ Example:
JsonObject jsonObject = ⚬ Example:
JsonArray jsonArray =
[Link]() [Link]()
.add("name", "Alice") .add("Apple")
.add("age", 28) .add("Banana")
.build(); .build();
String name = String fruit = [Link](0); //
[Link]("name"); // "Alice" "Apple"
SEPTEMBER,
2024
Key Classes in JSON
• JsonParser
⚬ Pull-based JSON parsingProcessing • JsonGenerator
⚬ Writes JSON data
using events (e.g., incrementally as a
START_OBJECT, stream.
KEY_NAME). ⚬ Suitable for generating
⚬ Efficient for large large JSON files.
datasets that can’t be ⚬ Example: generator =
JsonGenerator
fully loaded into memory.
JsonParser parser = [Link]([Link]);
[Link](new
⚬ Example: [Link]()
StringReader(jsonData));
while ([Link]()) {
.write("name", "John")
[Link] event = [Link](); .writeEnd();
if (event ==
[Link].KEY_NAME) {
[Link]([Link]());
}
SEPTEMBER,
2024
Key Classes in JSON
• JsonReader:
Processing
⚬ Reads JSON data from a stream and
converts it to JsonObject or JsonArray.
⚬ Example:JsonReader reader = [Link](new
StringReader(jsonData));
JsonObject jsonObject = [Link]();
• JsonWriter [Link]();
⚬ Writes a JsonObject or JsonArray to an
output stream.
⚬ Example:JsonWriter writer = [Link]([Link]);
[Link](jsonObject);
[Link]();
SEPTEMBER,
2024
Key Classes in JSON
• JsonReader:
Processing
⚬ Reads JSON data from a stream and
converts it to JsonObject or JsonArray.
⚬ Example:JsonReader reader = [Link](new
StringReader(jsonData));
JsonObject jsonObject = [Link]();
• JsonWriter [Link]();
⚬ Writes a JsonObject or JsonArray to an
output stream.
⚬ Example:JsonWriter writer = [Link]([Link]);
[Link](jsonObject);
[Link]();
SEPTEMBER,
2024
Summary of Key Classes:
• Object Model API Classes:
⚬ JsonObject, JsonArray, JsonReader, JsonWriter
⚬ Good for handling small to medium-sized JSON data in
memory.
• Streaming API Classes:
⚬ JsonParser, JsonGenerator
⚬ Efficient for handling large or streamed data, with low
memory usage.
SEPTEMBER,
2024
Thank you