All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Read JSON File In Java

Last Updated : Sep 7, 2023

How To Read JSON File In Java

In this article we will show you the solution of how to read JSON file in java, when working with data exchange and storage in Java programming, reading JSON files is a regular operation.

A popular lightweight data format for storing and sending structured data is JSON (JavaScript Object Notation).

When reading a JSON file in Java, the necessary libraries must be imported, and the functions of those libraries must be used to parse and extract the data from the file.

Java programmers have various choices for reading JSON files.

Using the JSON libraries offered by the Jackson library, which gives a comprehensive set of functionalities for working with JSON data, is one well-liked method.

Utilizing the built-in JSON parsing features available in Java SE 7 and subsequent versions provides an additional choice. Now we'll talk about the idea of reading JSON files in Java.

Step By Step Guide On How To Read JSON File In Java :-

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
public class ReadJSONFileExample {
    public static void main(String[] args) {
        // Specify the path to your JSON file
        String filePath = "path/to/your/json/file.json";
        // Create an ObjectMapper instance
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            JsonNode jsonNode = objectMapper.readTree(new File(filePath));
            // Access specific data elements in the JSON file
            String name = jsonNode.get("name").asText();
            int age = jsonNode.get("age").asInt();
            boolean isStudent = jsonNode.get("isStudent").asBoolean();
            // Print the retrieved data
            System.out.println("Name: " + name);
            System.out.println("Age: " + age);
            System.out.println("Is Student: " + isStudent);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  1. You can see that we wrote Java code to demonstrate how to read a JSON file in Java in this location.
  2. The first thing we do is import JsonNode and ObjectMapper from the com.fasterxml.jackson.databind package.
  3. We must use these classes in order to read and parse JSON files' data.
  4. For actions involving files, we also import classes from the java.io package, such as File and IOException.
  5. A class called ReadJSONFileExample is defined.
  6. We define and initialize a String variable called filePath with the path to the JSON file we wish to read inside the main method "path/to/your/json/file.json" needs to be changed to the real file location.
  7. We establish an ObjectMapper instance. The Jackson library's this class, which offers functions for writing and reading JSON data.
  8. In order to manage any exceptions, we employ a try-catch block.
  9. We use the ObjectMapper class' readTree function inside the try block to read the JSON file into a JsonNode object.
  10. A File object that represents the JSON file is required by the readTree function.
  11. We access particular data items from the JsonNode object after the JSON file has been read.
  12. In the example, we use JsonNode's get method to collect the information from the "name," "age," and "isStudent" fields.
  13. Then, using the asText(), asInt(), and asBoolean() methods, we convert them into the necessary data types.
  14. Using System.out.println(), we print the retrieved data components to the console.
  15. If an IOException occurs when reading a file, the catch block is run and e.printStackTrace() is used to print the stack trace of the exception to the standard error stream.

Conclusion :-

As a result, we have successfully learned the Java notion of reading JSON files.

We additionally discovered that utilizing the JSON libraries provided by the Jackson library, providing an extensive set of capabilities for interacting with JSON data, is one popular method.

I hope this article on how to read JSON file in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪