Java Structured Data: Reading and Writing JSON and CSV
Two Student records, Emma Clark with 91 in Java and Liam Brown with 78 in SQL, saved two ways. Jackson's ObjectMapper writes them to students.json and reads them back in one call each; plain Java writes students.csv with a header and reads it back by splitting at the commas.
What you will learn
- Describe how the same records look as JSON and as CSV
- Write a list of records to JSON and read it back with Jackson's ObjectMapper
- Write a CSV file with a header and comma-joined lines using Files.write
- Read a CSV file by skipping the header, splitting and parsing
Before you start
- Reading and writing text files in Java
- Java records
Imagine a record for every student, with a name, a course and a score, and thousands of them to save and load again. Two common text formats do the job: JSON and CSV. Every example uses this record:
package org.studyeasy.s07l03;
// One student: the same three values in JSON and in CSV
public record Student(String name, String course, int score) {}
The same two students, two ways
Here are Emma Clark (Java, 91) and Liam Brown (SQL, 78) as JSON:
[ {
"name" : "Emma Clark",
"course" : "Java",
"score" : 91
}, {
"name" : "Liam Brown",
"course" : "SQL",
"score" : 78
} ]
And as CSV:
name,course,score
Emma Clark,Java,91
Liam Brown,SQL,78
In JSON, square brackets hold a list and each pair of curly braces is one student, with every value named by a key. CSV is a header line with the column names, then one line per student with commas between the values. Both are plain text, so any program can read them.
Write JSON
package org.studyeasy.s07l03;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.util.List;
public class Ex01_WriteJson {
public static void main(String[] args) throws Exception {
List<Student> students = List.of(
new Student("Emma Clark", "Java", 91),
new Student("Liam Brown", "SQL", 78));
// Jackson's ObjectMapper turns objects into JSON
ObjectMapper mapper = new ObjectMapper();
mapper.writerWithDefaultPrettyPrinter()
.writeValue(new File("students.json"), students);
System.out.println("Saved students.json");
}
}
Saved students.json
Java has no built-in JSON writer, so the project adds Jackson (jackson-databind) through its pom.xml. An ObjectMapper takes objects in and gives JSON text out. One writeValue call writes the whole list: each record becomes one { } block, and the pretty printer adds the line breaks shown above.
Read JSON
ObjectMapper mapper = new ObjectMapper();
// a JSON array [ ... ] comes back as an array
Student[] students = mapper.readValue(
new File("students.json"), Student[].class);
for (Student student : students) {
System.out.println(student);
}
Student[name=Emma Clark, course=Java, score=91]
Student[name=Liam Brown, course=SQL, score=78]
readValue takes the file and Student[].class, which tells Jackson what to build from each block. The keys match the record's component names, so each block becomes a Student again, and the record's own toString prints it. Run Ex01 first so the file exists.
Write CSV
List<String> lines = new ArrayList<>();
lines.add("name,course,score"); // the header row
for (Student s : students) {
lines.add(s.name() + "," + s.course() + "," + s.score());
}
Files.write(Path.of("students.csv"), lines);
System.out.println("Saved students.csv");
Saved students.csv
This one is plain Java. A CSV file is just lines of text, so we build a List<String>: the header first, then each student's values joined with commas. Files.write saves all the lines in one call.
Read CSV
Path file = Path.of("students.csv");
List<String> lines = Files.readAllLines(file);
// line 0 is the header, so start at 1
for (int i = 1; i < lines.size(); i++) {
String[] cells = lines.get(i).split(",");
String name = cells[0];
int score = Integer.parseInt(cells[2]);
System.out.println(name + ": " + score);
}
Emma Clark: 91
Liam Brown: 78
readAllLines gives three strings. The loop starts at 1 to skip the header; start at 0 and parseInt meets the word "score", throwing NumberFormatException. split(",") cuts a line into cells, and every cell is text until you parse it. This simple split assumes no value contains a comma.
JSON or CSV?
| JSON | CSV | |
|---|---|---|
| One student | { "name" : "Emma Clark", ... } | Emma Clark,Java,91 |
| Labels | every value has a key | the header names the columns |
| Write and read | writeValue, readValue (Jackson) | Files.write, split(",") |
| Good for | objects, nested data, web APIs | tables, spreadsheets |
Rows and columns: CSV. Anything with more shape: JSON. Either way, it's text you save and read back.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Java Files and Directories with NIO.2: Path and Files
Path.of("invoices", "2026", "inv-101.pdf") is only an address: getFileName, getParent and resolve work, but nothing is on disk. Files.createDirectories and createFile make an order file and delete removes it, Files.walk finds 4 .java files, and os.name tells Windows from Linux.
Reading and Writing Text Files in Java
Files.readString reads tasks.txt in one call and readAllLines splits it into three tasks. writeString makes a shopping list that Files.lines filters to 2 items starting with B, a BufferedWriter writes an invoice, a Scanner totals 257, and Café is 4 characters but 5 bytes.
