Learn Java
import com.learnjava.filehandling.JsonData;

Working with JSON Data in Java

@Concept

JSON (JavaScript Object Notation) is a widely-used structured text format for representing nested data — objects with named fields, arrays, strings, numbers, booleans, and null — and unlike CSV, Java's standard library has no built-in JSON support at all, so working with JSON in Java virtually always means reaching for a third-party library. Jackson and Gson are the two most widely used: both can deserialize a JSON string directly into a matching Java object, mapping JSON fields to a class's fields by name automatically, and serialize a Java object back into a JSON string, in each case with just one or two method calls handling structure that would otherwise require substantial manual parsing code.

For this to work automatically, the target Java class typically needs a field, and often a getter or setter, matching each JSON field's name — though most libraries also offer configuration to remap a JSON field to a differently-named Java field when the names don't already line up, or to ignore fields that don't matter for a given use case.

@HowItWorks
1

What JSON represents

Nested objects, arrays, strings, numbers, booleans, and null, in a widely-used structured text format.

2

No built-in JSON support

Java's standard library has no JSON classes at all, so a third-party library is essentially required.

3

Jackson and Gson

The two most widely used third-party libraries for converting between JSON text and Java objects.

4

Deserializing

Converting a JSON string into a matching Java object, mapping JSON fields to Java fields by name automatically.

5

Serializing

The reverse — converting a Java object back into its JSON text representation.

@Example
import com.fasterxml.jackson.databind.ObjectMapper;
 
public class JsonDemo {
static class Person {
public String name;
public int age;
}
 
public static void main(String[] args) throws Exception {
String json = """
{"name": "Ana", "age": 30}
""";
ObjectMapper mapper = new ObjectMapper();
Person p = mapper.readValue(json, Person.class);
System.out.println("Name: " + p.name + ", Age: " + p.age);
}
}
Output
Name: Ana, Age: 30
1The text block, delimited by triple quotes, holds the raw JSON directly, with no need to escape any internal quote characters.
2mapper.readValue(json, Person.class) parses that JSON text and creates a new Person object, matching JSON fields to Person's fields by name.
3name and age on the resulting Person object hold the values read directly from the JSON, with no manual parsing code written.
4The single println call confirms both fields were correctly populated from the parsed JSON.
@UseCases
Parsing a JSON API response into a matching Java object automatically
Converting a Java object into JSON text to send in an HTTP request or response
Choosing between Jackson and Gson, the two most common Java JSON libraries
Remapping a JSON field to a differently-named Java field when they don't already match

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.