How to Write/create a 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

To create an array in a JSON file using a Java program −

  • Instantiate the JSONObject class of the json-simple library.
//Creating a JSONObject object
JSONObject jsonObject = new JSONObject();
  • Insert the required key-value pairs using the put() method of the JSONObject class.
jsonObject.put("key", "value");
  • Create a JSON array by instantiating the JSONArray class and add, elements to the created array using the add() method of the JSONArray class.
JSONArray array = new JSONArray();
array.add("element_1");
array.add("element_2");
array.add("element_3");
  • After adding all the required elements add the array into the JSON document using the put() method as −
jsonObject.put("contact",array);
  • Write the created JSON object into a file using the FileWriter class as −
FileWriter file = new FileWriter("E:/json_array_output.json");
file.write(jsonObject.toJSONString());
file.close();

Following Java program creates a JSON object with an array in it and writes it into a file named json_array_output.json.

Example

import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class WritingJSONArray {
   public static void main(String args[]) {
      //Creating a JSONObject object
      JSONObject jsonObject = new JSONObject();
      //Inserting key-value pairs into the json object
      jsonObject.put("ID", "1");
      jsonObject.put("First_Name", "Krishna Kasyap");
      jsonObject.put("Last_Name", "Bhagavatula");
      jsonObject.put("Date_Of_Birth", "1989-09-26");
      jsonObject.put("Place_Of_Birth", "Vishakhapatnam");
      jsonObject.put("Country", "25000");
      //Creating a json array
      JSONArray array = new JSONArray();
      array.add("e-mail: krishna_kasyap@gmail.com");
      array.add("phone: 9848022338");
      array.add("city: Hyderabad");
      array.add("Area: Madapur");
      array.add("State: Telangana");
      //Adding array to the json object
      jsonObject.put("contact",array);
      try {
         FileWriter file = new FileWriter("E:/json_array_output.json");
         file.write(jsonObject.toJSONString());
         file.close();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      System.out.println("JSON file created: "+jsonObject);
   }
}

Output

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

If you observe the contents of the JSON file you can see the created data as −

raja
Published on 03-Jul-2019 11:52:59
Advertisements