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):
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:
{
"name": "Sara",
"age": 25,
"active": true,
"roles": ["admin", "user"],
"address": {
"city": "Beirut"
}
}Note
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:
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
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>
<name>Sara</name>
<age>25</age>
<roles>
<role>admin</role>
<role>user</role>
</roles>
</user>Note
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(","):
String line = "Sara,25,Beirut";
String[] parts = line.split(",");
System.out.println("Name: " + parts[0]);
System.out.println("City: " + parts[2]);Watch out
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.