Phase 4 · Advanced JavaModule 25~32 min read

JSON, XML & Data Formats

Serialize and parse structured data with JSON, XML, and CSV, mapping it to Java objects.

What you'll learn

Programs constantly exchange data — with APIs, files, and each other. That data travels in structured formats. This module covers the big three (JSON, XML, CSV) and, most importantly, how to convert between them and Java objects.

By the end you'll be able to:

  • Read and write JSON — the format of modern web APIs
  • Serialize Java objects to JSON and deserialize them back (Jackson/Gson)
  • Recognise XML structure and when it's used
  • Parse CSV data

Structured data formats

Each format is text with agreed-upon rules for structure. They trade off readability, verbosity, and features — pick the one that matches the job (and, often, the one the other system requires):

The three you'll meet most

JSON

Compact, human-readable, the default for web APIs.

XML

Verbose but powerful; schemas and namespaces. Enterprise & config.

CSV

Rows of comma-separated values. Spreadsheets & bulk data.

JSON syntax

JSON (JavaScript Object Notation) is the lingua franca of web APIs. It has just a few building blocks: objects { } of key-value pairs, arrays [ ], strings, numbers, booleans, and null — which nest freely:

user.json
{
  "name": "Sara",
  "age": 25,
  "active": true,
  "roles": ["admin", "user"],
  "address": {
    "city": "Beirut"
  }
}

Note

JSON keys are always strings in double quotes. There are no comments and no trailing commas — two common gotchas when you write JSON by hand.

Mapping JSON to Java objects

You rarely work with JSON as raw text — you convert it to and from Java objects. The most popular library is Jackson (Gson is a lighter alternative). Its ObjectMapper turns an object into JSON (serialize) and JSON back into an object (deserialize), matching fields by name automatically:

Json.java
import com.fasterxml.jackson.databind.ObjectMapper;

record User(String name, int age) { }

ObjectMapper mapper = new ObjectMapper();

// Object -> JSON (serialize)
String json = mapper.writeValueAsString(new User("Sara", 25));
System.out.println(json);

// JSON -> Object (deserialize)
User u = mapper.readValue(json, User.class);
System.out.println(u.name() + " is " + u.age());

Tip

Records (Module 10) pair beautifully with JSON mapping — Jackson maps their components straight to JSON fields. Add Jackson to your project with a build tool (Module 31); it's a dependency, not part of the JDK.

XML

XML predates JSON and is more verbose, wrapping data in nested tags. It's still everywhere in enterprise systems, configuration files, and document formats, and it supports advanced features like schemas (validation) and namespaces:

user.xml
<user>
  <name>Sara</name>
  <age>25</age>
  <roles>
    <role>admin</role>
    <role>user</role>
  </roles>
</user>

Note

Java parses XML with built-in APIs: DOM (loads the whole tree into memory — easy but heavy), SAX (streaming — memory-efficient for huge files), and libraries like JAXB that map XML to objects the way Jackson maps JSON.

CSV

CSV (comma-separated values) is the simplest format — plain rows of fields — and the native language of spreadsheets. You can split simple CSV by hand with String.split(","):

Csv.java
String line = "Sara,25,Beirut";

String[] parts = line.split(",");
System.out.println("Name: " + parts[0]);
System.out.println("City: " + parts[2]);

Watch out

Real CSV is trickier than it looks — fields can contain commas inside quotes, embedded newlines, and escaped characters. For anything beyond trivial data, use a proper library (like Apache Commons CSV or OpenCSV) rather than splitting by hand.

Recap & quick check

Key takeaways

  • JSON is the default for web APIs: objects { }, arrays [ ], strings, numbers, booleans, null.
  • Use Jackson's ObjectMapper to serialize objects to JSON and deserialize JSON to objects.
  • Records map cleanly to JSON fields by name.
  • XML is verbose but feature-rich (schemas, namespaces); parse with DOM, SAX, or JAXB.
  • CSV is simple rows of values — use a library for real-world CSV, not just split(',').

Quick check

1. Which format is the default for most modern web APIs?

2. What does it mean to 'serialize' an object?

3. Which library commonly maps Java objects to JSON?

4. A valid JSON object uses which for keys?

5. Why avoid split(',') for real CSV files?

Nicely done — you can now exchange data with any system. Next up: Module 26 — Annotations & Reflection.