How to read JSON file in Java – JSONObject and JSONArray

What is JSON ?

JSON stands for JavaScript Object Notation. JSON is one of the widely used data-interchange format as it is a lightweight data-interchange format and language independent and it started to slowly replace the XML format. In this tutorial, we will see  how to use JSON.simple to read JSON file. We will be using JSONObject and JSONArray to perform the JSON read operation.

How to read JSON file in Java ?

We will read JSON file using JSON.simple library(json-simple.jar). JSON.simple can be used to encode or decode JSON text and Fully compliant with JSON specification (RFC4627).

As a pre-requisite, you are required to download the json-simple-1.1.1.jar (or) if you are running on maven add the below dependency to your pom.xml

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

JSON file content(sample.json)

{
    "Name": "www.javainterviewpoint.com",
    "Age": 999,
    "Countries": [
        "India",
        "England",
        "Australia"
    ]
}

JSON Reader

package com.javainterviewpoint;

import java.io.FileNotFoundException;
import java.io.FileReader;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class JSON_Reader
{
    public static void main(String args[])
    {
        JSONParser parser = new JSONParser();
        try
        {
            Object object = parser
                    .parse(new FileReader("c:\\sample.json"));
            
            //convert Object to JSONObject
            JSONObject jsonObject = (JSONObject)object;
            
            //Reading the String
            String name = (String) jsonObject.get("Name");
            Long age = (Long) jsonObject.get("Age");
            
            //Reading the array
            JSONArray countries = (JSONArray)jsonObject.get("Countries");
            
            //Printing all the values
            System.out.println("Name: " + name);
            System.out.println("Age: " + age);
            System.out.println("Countries:");
            for(Object country : countries)
            {
                System.out.println("\t"+country.toString());
            }
        }
        catch(FileNotFoundException fe)
        {
            fe.printStackTrace();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

We will be performing the below steps to read a JSON File in Java

  • Create a new object for the JSONParser, whose parse() method will hold the content of sample.json which is read through FileReader.
Object object = parser
                    .parse(new FileReader("c:\\Jackson\\sample.json"));
  • Typecast the Object which we have received from the parse() method into JSONObject type.
JSONObject jsonObject = (JSONObject)object;
  • Using the get() method of the JSONObject class get the individual values.
  String name = (String) jsonObject.get("Name");
  Long age = (Long) jsonObject.get("Age");
  • For reading the array of values, we will be using JSONArray class and read the individual values.
JSONArray countries = (JSONArray)jsonObject.get("Countries");

Output :

Once we run the above code we will be getting the below output.

Read a JSON File

 

 

 

 

How to convert String to JSON Object in Java

There are times instead of reading a JSON file, we will be getting a JSON response. Let’s now see how to convert String to JSON Object.

Let’s assume we are getting a JSON response from a Webservice like below

{“Name”:”Javainterviewpoint”,”Age”:”999″} 

package com.javainterviewpoint;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class JSON_Reader
{
    public static void main(String args[])
    {
        String jsonString = "{\"Name\":\"Javainterviewpoint\",\"Age\":\"100\"}";
        JSONParser parser = new JSONParser();
        try
        {
            Object object = parser
                    .parse(jsonString);
            
            //convert Object to JSONObject
            JSONObject jsonObject = (JSONObject)object;
            
            //Reading the String
            String name = (String) jsonObject.get("Name");
            String age = (String) jsonObject.get("Age");
            
            //Printing the values
            System.out.println("Name: " + name);
            System.out.println("Age: " + age);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

Output:

Name: Javainterviewpoint
Age: 999

Leave a Reply

Your email address will not be published. Required fields are marked *