VOOZH about

URL: https://www.javacodegeeks.com/java-json-to-pojo-conversion.html

⇱ Java JSON to POJO Conversion - Java Code Geeks


JSON is a lightweight format used for data exchange between systems. In Java, mapping JSON to a POJO makes your code cleaner, type-safe, and easier to maintain. The example demonstrates three ways to convert a JSON string into a Java object: manual parsing using org.json, automatic mapping using Jackson, and automatic mapping using Gson. All three approaches produce the same output, but Jackson and Gson are more concise and ideal for real-world applications. Let us delve into understanding how Java JSON to POJO conversion works.

1. Introduction to JSON

JSON (JavaScript Object Notation) is a widely used lightweight data format for communication between systems. Mapping JSON to a POJO makes your Java application more type-safe, readable, and easy to maintain.

2. Code Example

2.1 Adding Dependencies (pom.xml)

<dependencies>

 <!-- org.json -->
 <dependency>
 <groupId>org.json</groupId>
 <artifactId>json</artifactId>
 <version>stable__jar__version</version>
 </dependency>

 <!-- Jackson -->
 <dependency>
 <groupId>com.fasterxml.jackson.core</groupId>
 <artifactId>jackson-databind</artifactId>
 <version>stable__jar__version</version>
 </dependency>

 <!-- Gson -->
 <dependency>
 <groupId>com.google.code.gson</groupId>
 <artifactId>gson</artifactId>
 <version>stable__jar__version</version>
 </dependency>

</dependencies>

2.2 Code Example

The following complete Java example demonstrates manual JSON parsing as well as automated JSON-to-POJO mapping using Jackson and Gson.

// JsonMappingDemo.java
import org.json.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;

public class JsonMappingDemo {

 // ----- POJO -----
 public static class User {
 private int id;
 private String name;
 private boolean active;

 // getters/setters
 public int getId() { return id; }
 public void setId(int id) { this.id = id; }

 public String getName() { return name; }
 public void setName(String name) { this.name = name; }

 public boolean isActive() { return active; }
 public void setActive(boolean active) { this.active = active; }

 @Override
 public String toString() {
 return "User{" +
 "id=" + id +
 ", name='" + name + '\'' +
 ", active=" + active +
 '}';
 }
 }

 // ----- MAIN -----
 public static void main(String[] args) throws Exception {

 String json = "{ \"id\": 101, \"name\": \"Alice\", \"active\": true }";

 // 1. Manual Mapping using org.json
 JSONObject jsonObj = new JSONObject(json);
 User manualUser = new User();
 manualUser.setId(jsonObj.getInt("id"));
 manualUser.setName(jsonObj.getString("name"));
 manualUser.setActive(jsonObj.getBoolean("active"));

 System.out.println("=== Manual Mapping Output ===");
 System.out.println(manualUser);


 // 2. Mapping using Jackson
 ObjectMapper mapper = new ObjectMapper();
 User jacksonUser = mapper.readValue(json, User.class);

 System.out.println("\n=== Jackson Mapping Output ===");
 System.out.println(jacksonUser);


 // 3. Mapping using Gson
 Gson gson = new Gson();
 User gsonUser = gson.fromJson(json, User.class);

 System.out.println("\n=== Gson Mapping Output ===");
 System.out.println(gsonUser);
 }
}

2.2.1 Code Explanation

The given Java program demonstrates three different ways to map a JSON string into a Java POJO using org.json, Jackson, and Gson. It begins by importing the necessary classes—JSONObject from org.json for manual parsing, ObjectMapper from Jackson for automatic JSON-to-POJO conversion, and Gson from Google’s Gson library. Inside the JsonMappingDemo class, an inner static POJO named User is defined with fields id, name, and active, along with their getters and setters, ensuring proper encapsulation. The toString() method is overridden to print readable output. In the main method, a JSON string is declared containing the user attributes. The first approach, manual mapping with org.json, creates a JSONObject from the JSON string and manually extracts each value using getInt(), getString(), and getBoolean() to populate a new User object. The program prints the mapped object to show the result. The second approach uses Jackson’s ObjectMapper, which automatically converts the JSON into a User object using readValue(), eliminating manual extraction. The output is printed to confirm successful mapping. The third approach uses Gson by creating a Gson instance and calling fromJson(), which performs similar automatic binding to produce another User object. Finally, all three mapped objects are printed to compare the outputs, demonstrating that each technique successfully converts JSON into a Java POJO, though the library-based methods are more concise and reduce boilerplate code.

2.2.2 Code Output

=== Manual Mapping Output ===
User{id=101, name='Alice', active=true}

=== Jackson Mapping Output ===
User{id=101, name='Alice', active=true}

=== Gson Mapping Output ===
User{id=101, name='Alice', active=true}

The output shows that all three mapping approaches—manual parsing with org.json, automatic deserialization using Jackson, and automatic deserialization using Gson—produce the exact same result, confirming that each technique correctly converts the JSON string into a User POJO. The first section, labeled “Manual Mapping Output,” prints the User object created by manually extracting individual fields from the JSONObject. The second section, “Jackson Mapping Output,” demonstrates how Jackson’s ObjectMapper automatically maps JSON fields to the POJO without any manual extraction. The third section, “Gson Mapping Output,” shows a similar result produced by Gson’s fromJson() method. All three outputs display User{id=101, name='Alice', active=true}, confirming that the JSON was interpreted consistently across all methods and that the POJO mapping is accurate and reliable regardless of which library is used.

3. Conclusion

Using org.json, you can perform manual mappings, giving full control but requiring more boilerplate. Jackson and Gson offer convenient automatic mapping with fewer lines of code and better maintainability. For production systems, Jackson or Gson is recommended.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

👁 Photo of Yatin Batra
Yatin Batra
December 15th, 2025Last Updated: December 15th, 2025
0 305 3 minutes read

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz