POJO vs JSON: What's the Difference?
New to Java? Confused about POJOs and JSON? This guide explains both concepts, their differences, and how they work together.
TL;DR Quick Answer
POJO (Plain Old Java Object)
A Java class in memory. It has fields, getters, setters, and methods. Lives in your code.
JSON (JavaScript Object Notation)
A text format for data. It's a string that represents data. Used for APIs, files, storage.
Side-by-Side Comparison
POJO (Java Class)
public class User {
private String name;
private int age;
// Getters
public String getName() {
return name;
}
public int getAge() {
return age;
}
// Setters
public void setName(String name) {
this.name = name;
}
}JSON (Text Format)
{
"name": "John Doe",
"age": 30
}Just a string! Can be saved to a file, sent over HTTP, or stored in a database.
Key Differences
| Aspect | POJO | JSON |
|---|---|---|
| What is it? | Java class (code) | Text format (data) |
| Where it lives | In JVM memory | Files, APIs, databases |
| Can have methods? | Yes | No (data only) |
| Type safety | Compile-time | Runtime only |
| Language | Java/Kotlin | Language-agnostic |
How They Work Together
In real applications, you constantly convert between POJO and JSON:
POJO
In-memory object
→ serialize← deserialize
JSON
Text for transfer
- API Response: Server sends JSON → You deserialize to POJO
- API Request: You serialize POJO → Send JSON to server
- File Storage: Serialize POJO → Save JSON to file
Converting Between POJO and JSON
JSON → POJO (Deserialization)
ObjectMapper mapper = new ObjectMapper();
String json = "{\"name\":\"John\"}";
// Parse JSON to POJO
User user = mapper.readValue(
json, User.class);
System.out.println(user.getName());
// Output: JohnPOJO → JSON (Serialization)
ObjectMapper mapper = new ObjectMapper();
User user = new User();
user.setName("Jane");
// Convert POJO to JSON
String json = mapper
.writeValueAsString(user);
System.out.println(json);
// Output: {"name":"Jane"}When to Use Each
Use POJO When:
- • Working with data inside your Java app
- • Need methods and business logic
- • Want compile-time type safety
- • Building domain models
Use JSON When:
- • Sending data over HTTP/APIs
- • Storing data in files/databases
- • Communicating with other languages
- • Configuration files
Convert JSON to POJO Instantly
Skip the manual class creation. Paste your JSON and get Java POJOs in seconds.