Java write data to JSON file
In this post, you will learn how to write data to a JSON file using the Java programming language.
JSON (JavaScript Object Notation) is popular among developers for data serialization. It is a data format consisting of key-value pairs. It is widely used to transmit and receive data between a server and a web application in JSON format. It is so popular that every modern programming language has methods to generate and parse JSON formatted data.
Java JSON.simple library
Java provides several libraries to read and write data to a JSON file. JSON.simple is a simple Java based library for JSON. It is a lightweight, streaming supported and provide high performance.
Install json.simple
To install json.simple, we need to set classpath of json-simple.jar or add the Maven dependency.
Download json-simple.jarOr, to add the maven dependency, write the following code in pom.xml file.
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
Java write data to JSON file
To write data to a JSON document using Java, first we need to instantiate the JSONObject class of the json-simple library.
//Creating a JSONObject object
JSONObject jsonObject = new JSONObject();
The JSONObject class provides the put() method to insert the required key-value pairs.
jsonObject.put("key", "value");
We use the FileWriter class of the java.io package to write data in character form to a file.
FileWriter file = new FileWriter();
Complete code
Here is the complete Java code to write data to a JSON file using the json.simple library.
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class FirstProgram{
public static void main(String[] args) {
// creating JSONObject
JSONObject jsnObj = new JSONObject();
// putting data to JSONObject
jsnObj.put("FirstName", "Henry");
jsnObj.put("LastName", "Goldy");
jsnObj.put("Age", 35);
// creating LinkedHashMap
// for address
Map hm = new LinkedHashMap(4);
hm.put("Street", "25 FR 4th Street");
hm.put("City", "Ajaccio");
hm.put("State", "Corsica");
hm.put("PostalCode", 20199);
// putting address to JSONObject
jsnObj.put("Address", hm);
// creating JSONArray
JSONArray arr = new JSONArray();
hm = new LinkedHashMap(2);
hm.put("type", "home");
hm.put("number", "33-15-4233");
// adding map to array
arr.add(hm);
jsnObj.put("MobileNumber", arr);
// writing Data to JSON file
try (FileWriter file = new FileWriter("c://java/demo.json")) {
file.write(jsnObj.toJSONString());
file.flush();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Output of the above code:
demo.json
{"MobileNumber":[{"type":"home","number":"33-15-4233"}],"Address":{"Street":"25 FR 4th Street","City":"Ajaccio","State":"Corsica","PostalCode":20199},"FirstName":"Henry","LastName":"Goldy","Age":35}
Related Articles
Number pattern programs in JavaJava program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number