How to read/parse JSON array using Java?

JavaObject Oriented ProgrammingProgramming

A Json array is an ordered collection of values that are enclosed in square brackets i.e. it begins with ‘[’ and ends with ‘]’. The values in the arrays are separated by ‘,’ (comma).

Sample JSON array

{
   "books": [ Java, JavaFX, Hbase, Cassandra, WebGL, JOGL]
}

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>
</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 and an array as shown below −

{
   "ID": "1",
   "First_Name": "Krishna Kasyap",
   "Last_Name": "Bhagavatula",
   "Date_Of_Birth": "1989-09-26",
   "Place_Of_Birth":"Vishakhapatnam",
   "Salary": "25000"
   "contact": [
      "e-mail: krishna_kasyap@gmail.com",
      "phone: 9848022338",
      "city: Hyderabad",
      "Area: Madapur",
      "State: Telangana"
   ]
}

To read an array from 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");
  • Just like other element retrieve the json array using the get() method into the JSONArray object.
JSONArray jsonArray = (JSONArray) jsonObject.get("contact");
  • The iterator() method of the JSONArray class returns an Iterator object using which you can iterate the contents of the current array.
//Iterating the contents of the array
Iterator<String> iterator = jsonArray.iterator();
while(iterator.hasNext()) {
   System.out.println(iterator.next());
}

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 java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class ReadingArrayFromJSON {
   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:/test.json"));
         //Forming URL
         System.out.println("Contents of the JSON are: ");
         System.out.println("ID: "+jsonObject.get("ID"));
         System.out.println("First name: "+jsonObject.get("First_Name"));
         System.out.println("Last name: "+jsonObject.get("Last_Name"));
         System.out.println("Date of birth: "+ jsonObject.get("Date_Of_Birth"));
         System.out.println("Place of birth: "+ jsonObject.get("Place_Of_Birth"));
         System.out.println("Salary: "+jsonObject.get("Salary"));
         //Retrieving the array
         JSONArray jsonArray = (JSONArray) jsonObject.get("contact");
         System.out.println("");
         System.out.println("Contact details: ");
         //Iterating the contents of the array
         Iterator<String> iterator = jsonArray.iterator();
         while(iterator.hasNext()) {
            System.out.println(iterator.next());
         }
      } 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: Krishna Kasyap
Last name: Bhagavatula
Date of birth: 1989-09-26
Place of birth: Vishakhapatnam
Salary: 25000
Contact details:
e-mail: krishna_kasyap@gmail.com
phone: 9848022338
city: Hyderabad
Area: Madapur
State: Telangana
raja
Published on 03-Jul-2019 11:43:10
Advertisements