How to write JSON object to File in Java?

In my previous article we have learnt How to read JSON file in Java, now let’s see how we can write JSON Object to File in Java. Here also we will be using the JSON.simple library 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>


Let us try to create the same JSON which we have used in our previous example(sample.json) which will be having the below content.

JSON file content(sample.json)

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

How to write JSON object to File ?

package com.javainterviewpoint;

import java.io.FileWriter;

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

public class JSON_Writer
{
    public static void main(String args[])
    {
        try
        {
            // Create a new JSONObject
            JSONObject jsonObject = new JSONObject();

            // Add the values to the jsonObject
            jsonObject.put("Name", "www.javainterviewpoint.com");
            jsonObject.put("Age", "999");

            // Create a new JSONArray object
            JSONArray jsonArray = new JSONArray();

            // Add values to the jsonArray
            jsonArray.add("India");
            jsonArray.add("England");
            jsonArray.add("Australia");

            // Add the jsoArray to jsonObject
            jsonObject.put("Countries", jsonArray);

            // Create a new FileWriter object
            FileWriter fileWriter = new FileWriter("c:\\sample.json");

            // Writting the jsonObject into sample.json
            fileWriter.write(jsonObject.toJSONString());
            fileWriter.close();

            System.out.println("JSON Object Successfully written to the file!!");

        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

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

  • Create a new object for JSONObject, using the put() method of the jsonObject the add they key and value pairs into it.
JSONObject jsonObject = new JSONObject();
jsonObject.put("Name", "www.javainterviewpoint.com");
jsonObject.put("Age", "999");
  • Create a new object for JSONArray to add the list of countries, using the add() method add the countries into it.
 JSONArray jsonArray = new JSONArray();
 jsonArray.add("India");
 jsonArray.add("England");
 jsonArray.add("Australia");
  • Now, add the jsonArray into jsonObject
jsonObject.put("Countries", jsonArray);
  • Finally, create a new object for FileWriter and using the write() method write the jsonObject into the file.
fileWriter.write(jsonObject.toJSONString());

Output :

When we open the sample.json file, we will be having the JSON written in it.

Write JSON Object to File

Comments

  1. ankush says

    Actually I tried to write json into file using above code but its create file only ,data have not present in file, even it not gave error etc.

Leave a Reply

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