How to read the contents of a JSON file using Java?

JavaObject Oriented ProgrammingProgramming

JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc. Sample JSON document

{
   "book": [
      {
         "id": "01",
         "language": "Java",
         "edition": "third",
         "author": "Herbert Schildt"
      },
      {
         "id": "07",
         "language": "C++",
         "edition": "second",
         "author": "E.Balagurusamy"
      }
   ]
}

Json-simple library

The json-simple is a light weight library which is used to process JSON objects. Using this you can read or, write the contents of a JSON document using Java program.

JSON-Simple maven dependency

Following is the maven dependency for the JSON-simple library −

<dependencies>
   <dependency>
      <groupId>com.googlecode.json-simple</groupId>
      <artifactId>json-simple</artifactId>
      <version>1.1.1</version>
   </dependency> 301 to 305
</dependencies>

Paste this with in the <dependencies> </dependencies> tag at the end of your pom.xml file. (before </project> tag)

Example

First of all, let us create a JSON document with name sample.json with the 6 key-value pairs as shown below −

{
   "ID": "1",
   "First_Name": "Shikhar",
   "Last_Name": "Dhawan",
   "Date_Of_Birth": "1981-12-05",
   "Place_Of_Birth":"Delhi",
   "Country": "India"
}

To read the contents of a JSON file using a Java program −

  • Instantiate the JSONParser class of the json-simple library.
JSONParser jsonParser = new JSONParser();
  • Parse the contents of the obtained object using the parse() method.
//Parsing the contents of the JSON file
JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("E:/players_data.json"));
  • Retrieve the value associated with a key using the get() method.
String value = (String) jsonObject.get("key_name");

Following Java program parses the above created sample.json file, reads its contents and, displays them.

Example

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class ReadingJSON {
   public static void main(String args[]) {
      //Creating a JSONParser object
      JSONParser jsonParser = new JSONParser();
      try {
         //Parsing the contents of the JSON file
         JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("E:/sample.json"));
         String id = (String) jsonObject.get("ID");
         String first_name = (String) jsonObject.get("First_Name");
         String last_name = (String) jsonObject.get("Last_Name");
         String date_of_birth = (String) jsonObject.get("Date_Of_Birth");
         String place_of_birth = (String) jsonObject.get("Place_Of_Birth");
         String country = (String) jsonObject.get("Country");
         //Forming URL
         System.out.println("Contents of the JSON are: ");
         System.out.println("ID :"+id);
         System.out.println("First name: "+first_name);
         System.out.println("Last name: "+last_name);
         System.out.println("Date of birth: "+date_of_birth);
         System.out.println("Place of birth: "+place_of_birth);
         System.out.println("Country: "+country);
         System.out.println(" ");
      } catch (FileNotFoundException e) {
            e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      } catch (ParseException e) {
         e.printStackTrace();
      }
   }
}

Output

Contents of the JSON are:
ID :1
First name: Shikhar
Last name: Dhawan
Date of birth :1981-12-05
Place of birth: Delhi
Country: India
raja
Published on 03-Jul-2019 11:36:44
Advertisements