1 of 16

UNIT - 3

Object Model API

Consuming & Producing JSON

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

2 of 16

What is the Object Model API?

  • Part of JSON Processing (JSR 374 / javax.json / jakarta.json)
  • Represents JSON data in memory using a tree-like structure
  • Allows:
    • Random access to elements
    • Programmatic modification
    • Easy traversal
  • Similar to DOM for XML

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

3 of 16

Core Data Types

  • javax.json / jakarta.json
    • JsonObject → represents JSON object ({ })
    • JsonArray → represents JSON array ([ ])
    • JsonValue → JSON value (object, array, string, number, true/false, null)
    • JsonReader → reads JSON into a model
    • JsonWriter → writes JSON from a model
    • Json / JsonBuilderFactory → builder utilities

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

4 of 16

Object Model vs Streaming API

Object Model API

Streaming API

In-memory tree

Event-based cursor

Easy to modify

Read-only/forward

Higher memory usage

Low memory usage

Good for small JSON

Good for large JSON

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

5 of 16

Consuming JSON Using the Object Model API

Steps to Read JSON (Consume)

  1. Obtain an InputStream / Reader
  2. Create JsonReader
  3. Parse into JsonObject / JsonArray
  4. Extract values with getters
  5. Close reader

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

6 of 16

Example JSON

{

"id": 101,

"name": "Alice",

"skills": ["Java", "JSON", "REST"]

}

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

7 of 16

Code: Consuming JSON

JsonReader reader = Json.createReader(new FileInputStream("data.json"));

JsonObject obj = reader.readObject();

int id = obj.getInt("id");

String name = obj.getString("name");

JsonArray skills = obj.getJsonArray("skills");

System.out.println(name + " has skills: " + skills);

reader.close();

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

8 of 16

Accessing Array Elements

for (JsonValue skill : skills) {

System.out.println(skill.toString());

}

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

9 of 16

Modifying JSON Tree

JsonObject updated = Json.createObjectBuilder(obj)

.add("active", true)

.build();

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

10 of 16

Producing JSON Using the Object Model API

  • Steps to Produce JSON
  • Create a JsonObjectBuilder / JsonArrayBuilder
  • Add keys/values
  • Build a JsonObject / JsonArray
  • Use JsonWriter to output as JSON

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

11 of 16

Code: Building a JSON Object

JsonObject person = Json.createObjectBuilder()

.add("id", 102)

.add("name", "Bob")

.add("skills", Json.createArrayBuilder()

.add("Python")

.add("AI")

.add("ML"))

.build();

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

12 of 16

Writing to File

JsonWriter writer =

Json.createWriter(new FileOutputStream("output.json"));

writer.writeObject(person);

writer.close();

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

13 of 16

Output JSON

{

"id": 102,

"name": "Bob",

"skills": ["Python", "AI", "ML"]

}

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

14 of 16

Combining Consumption & Production

  • Object Model API enables:
    • Read JSON
    • Modify fields
    • Add/remove keys
    • Write back to file
    • Transform JSON structures
  • Example use cases:
    • REST services
    • Configuration files
    • Data transformation pipelines

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

15 of 16

Advantages of Object Model API

  • Intuitive tree navigation
  • Simple and readable code
  • Full programmatic control
  • Ideal for:
    • Small–medium JSON files
    • Applications that modify JSON frequently

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/

16 of 16

Limitations

  • More memory usage
  • Not suited for huge JSON streams
  • Slower for large datasets

https://ravulakartheek.blogspot.com/

https://ravulakartheek.blogspot.com/