How to Parse JSON in Java?

Parsing JSON in Java sounds simple until you are confused about choosing between Jackson, Gson, org.json, JSON.simple, or JSON-P. Which one is right for your project? How do you handle nested JSON or map it to a Java object cleanly? If you have ever felt unsure or copied code without truly understanding it, this guide is for you.

In this blog, learn how to parse JSON in Java with clear examples, simple explanations, and all the tools you need.

Table of Contents:

What is JSON?

JSON (JavaScript Object Notation) is a lightweight format for storing and transporting data. It is used for transporting data from the server to the web. It is easy to read and write, and is also easy for machines to parse and generate. It represents data in the form of key-value pairs or arrays.

Example:

{
  "id": 101,
  "name": "Intellipaat",
  "email": "[email protected]",
  "isActive": true,
  "roles": ["user", "admin"],
  "address": {
    "city": "Bareilly",
    "zip": "1234x"
  }
}

where,

  • Objects are enclosed in {} and made up of key-value pairs.
  • Arrays are enclosed in [] and are used to hold lists.
  • Keys are always strings (in double quotes).
  • Values can be strings, numbers, booleans, arrays, objects, or null.

Why Parse JSON in Java?

In modern Java applications, like Microservices, that use the API for the transfer of data between web pages, the data is exchanged in the form of JSON. Java does not natively understand the JSON format. Hence, parsing JSON means converting the raw JSON into JAVA objects so that the communication between the websites can be easy and fast.

Let us understand how we can parse the JSON object in Java using different methods and libraries.

Master Java Today - Accelerate Your Future
Enroll Now and Transform Your Future
quiz-icon

How to Parse JSON in Java

1. Parsing JSON from a String

JsonParser is a class in the Gson library (com.google.gson) that helps parse JSON text into JsonElement objects for further processing.  While useful, many developers today prefer higher-level parsing methods like Gson.fromJson() or Jackson’s ObjectMapper.

The parsing can be done from the string. Below is an example of it.

Step 1: Create a Maven project using IntelliJ or any other IDE.

Step 2: Add the following gson dependency in the pom.xml file.

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.10.1</version>
</dependency>

Step 3: Run the Maven clean and package command that will install all the dependencies from the pom.xml file

Step 4: Here is your Java file.

Java

Step 5: Run the file.

Output:

Parsing JSON from a string

2. Parsing JSON from an Array

JSON often contains arrays, and this data can also be parsed from the array.

Example:

Java

Output:

Parsing JSON from an Array

Explanation: This program helps to read a JSON array and print each person’s name and age. It changes the JSON text into a JSONArray, then picks each object and shows its values. It can run on any Java compiler with the org.json library.

3. Parsing JSON from a File by Creating POJO

In the real world, the data not only comes with strings, but it also comes in the form of files.

Step 1: Create a Maven project using IntelliJ or any other IDE.

Step 2: Add the following Jackson dependency to your pom.xml file to use the ObjectMapper class for parsing JSON into Java POJOs

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.16.1</version>
</dependency>

Step 3: Run the Maven clean and package command that will install all the dependencies from the pom.xml file

Step 4: Create user.json.

{
  "userId": 1,
  "name": "Ali"
}

Step 5: Create the POJO

Java

Step 6: Create the Main file.

Java

Step 7: Run the file.

Output:

Parsing JSON from a File by Creating POJO

Note: Files should be in the src/main/resources folder to ensure they are loaded properly.

4. Parsing JSON Object in Java

To parse a JSONObject in Java, you can use libraries such as Jackson or Gson, as mentioned above.

Here is an example of it by using the Jackson library.

Add the following Maven dependencies in the pom.xml file.

<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.16.0</version>
</dependency>

Example:

Java

Note: In real projects, POJOs are placed in separate files for better structure.

Output:

Parsing JSONObject in Java

5. Parsing JSON from an API Using RestAssured’s Native Parsing

RestAssured supports automatic JSON parsing via its built-in JSONPath and integrates with Jackson/Gson as needed. It is a popular Java library used for testing RESTful web services. It has automatic response parsing, i.e., it can deserialise HTTP response bodies (JSON and XML) into Java objects.

Step 1: Create a Maven project using IntelliJ or any other IDE.

Step 2: Add the following Jackson dependency in the pom.xml file.

<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.17.0</version>
        </dependency>

<dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>5.4.0</version>
</dependency>

Step 3: Run the Maven clean and package command that will install all the dependencies from the pom.xml file

Step 4: Here is your Java file.

Java

Note: In real projects, POJOs are placed in separate files for better structure.

Step 5: Run the file.

Output:

Parsing JSON from an API Using RestAssured’s Native Parsing
Unlock Your Future in Java
Start Your Java Journey for Free Today
quiz-icon

Parsing JSON Libraries in Java

1. org.json

The org.json library provides a simple way to parse and create JSON in Java. It can also convert between JSON, XML, HTTP Headers, Cookies, Comma Delimited List or Text, etc.

Step 1: Create a Maven project using IntelliJ or any other IDE.

Step 2: Add the following org.json dependency in the pom.xml file.

<dependency>
  <groupId>org.json</groupId>
  <artifactId>json</artifactId>
  <version>20231013</version>
</dependency>

Step 3: Run the Maven clean and package command that will install all the dependencies from the pom.xml file.

Step 4: Here is your Java file.

Java

Step 5: Run the file.

Output:

org.json

2. Jackson

Jackson is a high-performance JSON processor for Java. It can read JSON as a tree (like DOM for XML) and can also work with JSON in a streaming manner. It can handle complex data structures and large JSON files.

Step 1: Create a Maven project using IntelliJ or any other IDE.

Step 2: Add the following Jackson dependency in the pom.xml file.

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.16.0</version>
</dependency>

Step 3: Run the Maven clean and package command that will install all the dependencies from the pom.xml file

Step 4: Here is your Java file.

Java

Step 5: Run the file.

Output:

Jackson

3. Gson by Google

Gson is a Java library developed by Google for converting Java objects to JSON and vice versa. It can also be used to convert a JSON string to a Java object. It is simple and lightweight.  It parses JSON without using a big or complex library.

Step 1: Create a Maven project using IntelliJ or any other IDE.

Step 2: Add the following Gson dependency in the pom.xml file.

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.10.1</version>
</dependency>

Step 3: Run the Maven clean and package command that will install all the dependencies from the pom.xml file

Step 4: Here is your Java file.

Java

Step 5: Run the file.

Output:

Gson by Google

4. JSON.simple

Step 1: Create a Maven project using IntelliJ or any other IDE.

Step 2: Add the following json.simple dependency in the pom.xml file for the Maven project.

<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

For Gradle, add the following to your build.gradle file.

implementation 'com.googlecode.json-simple:json-simple:1.1.1'

Step 3: Run the Maven clean and package command that will install all the dependencies from the pom.xml file. For Gradle, reload the project to install all the dependencies in the project.

Step 4: Here is your Java file.

Java

Step 5: Run the file.

Output:

JSON.simple

5. JSON-P (Java API for JSON Processing)

JSON-P is a standard API to parse JSON and object models. It is not as popular as Gson or Jackson, but it is a part of the standard Java EE API.

Step 1: Create a Maven project using IntelliJ or any other IDE.

Step 2: Add the following json.simple dependency in the pom.xml file for the Maven project.

<dependencies>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.parsson</groupId>
<artifactId>parsson</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>

Step 3: Run the Maven clean and package command that will install all the dependencies from the pom.xml file.

Step 4: Here is your Java file.

Java

Step 5: Run the file.

Output:

JSON-P (Java API for JSON Processing

JSON Parsing Libraries Comparison in Java

Library Key Characteristics Pros Cons
org.json Simple and lightweight with JSONObject and JSONArray parsing Easy to use, no annotations or setup required No streaming support, manual POJO conversion, limited features
Jackson High-performance with Data Binding, Tree Model, and Streaming APIs Very fast, excellent object mapping, handles large files well Larger dependency size, requires annotations for complex mappings
Gson Google-developed with Data Binding and Tree Model support Lightweight, easy object conversion, less boilerplate Slower than Jackson for large datasets
JSON.simple Basic and lightweight with manual JSON construction/parsing Extremely lightweight, good for beginners and small tasks No POJO binding, lacks advanced features
JSON-P Jakarta EE standard with Streaming, Object Model, and Generator APIs No third-party dependencies, standard API, supports multiple parsing methods Slower performance, limited community support outside Jakarta EE

Conclusion

From the above, we conclude that JSON is used to get or send data from web services or APIs. Java has many libraries that can help you to parse the JSON. Jackson is a powerful and widely used library that can convert JSON to Java objects and back. Gson is also popular and can be very easy to use. The org.json library is simple and can be helpful for small projects. JSON.simple can be a good choice if you are just starting. JSON-P might be useful when you are working with Java EE or web applications. 

If you want to learn more about Java, you can refer to our Java Course.

Get 100% Hike!

Master Most in Demand Skills Now!

How to Parse JSON in Java – FAQs

Q1. How to parse a JSON file in Java?

Use the JsonParser.parse() to handle JSON strings or files and extract values from them. In Java, you can create a JsonReader using the JSON. createReader() method, which reads the JSON data and parses it into a JsonObject or JsonArray

Q2. How to parse an object to JSON in Java?

Create an object of the POJO class and set the required values in it using the setter methods.

Q3. How to extract data from JSON in Java?

First, create a Gson object and then use its fromJson() method to deserialize the JSON string to a JsonObject.

Q4. What is the full form of JSON?

JSON stands for JavaScript Object Notation.

Q5. How to access a value in a JSON object?

To access a value in a JSON object, you can use bracket and dot syntax.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner