Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Create JSON File Using Java



In this article, we will learn to write/create a JSON file using Java. JSON has become the standard for data exchange for any kind of application. Java also has a collection of libraries helpful in creating and storing JSON files.

JSON

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

Below is an example of a JSON file:

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

Different Approaches

The following are the different Libraries for writing/creating a JSON file in Java:

Using the 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 a Java program.

JSON-Simple maven dependency

The 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 within the <dependencies> </dependencies> tag at the end of your "pom.xml" file. (before </project> tag)

Using the Jackson Library

Jackson is a fast library for working with JSON in Java. It's good for big or complex projects that need speed and powerful features.

The Following is the Jackson dependency for reading/parsing a JSON array:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.15.2</version>
</dependency>

Using the Gson Library

Gson is a JSON library from Google. It's easy to use and works well for small projects where simplicity matters more than speed.

The Following is the Gson dependency for reading/parsing a JSON array:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.10.1</version>
</dependency>

Writing/Creating a JSON file

To create a JSON document using a Java program, the following are the steps:

Instantiate the JSONObject class of the JSON-Simple library.

JSONObject jsonObject = new JSONObject();

Insert the required key-value pairs using the put() method of the JSONObject class.

jsonObject.put("key", "value");

Write the created JSON object into a file using the FileWriter class as:

FileWriter file = new FileWriter("E:/output.json");
file.write(jsonObject.toJSONString());
file.close();

Example

Below is an example of a Java program that creates a JSON object and writes it into a file named output.json.

import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONObject;
public class CreatingJSONDocument {
   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", "Shikhar");
      jsonObject.put("Last_Name", "Dhawan");
      jsonObject.put("Date_Of_Birth", "1981-12-05");
      jsonObject.put("Place_Of_Birth", "Delhi");
      jsonObject.put("Country", "India");
      try {
         FileWriter file = new FileWriter("E:/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":"Shikhar",
"Place_Of_Birth":"Delhi",
"Last_Name":"Dhawan",
"Country":"India",
"ID":"1",
"Date_Of_Birth":
"1981-12-05"}

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

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-05-07T18:33:22+05:30

62K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements