Home »
Java programs
Java program to write and read a JSON File
Create, write and read in/from JSON file using java: Here, we are going to create two java programs, 1) program that will write content in JSON file and 2) program that will read content from the JSON file.
Submitted by Jyoti Singh, on January 31, 2018
JSON is in an array format which holds the key-value pairs. We can do this by using JSON simple library. Just download the library from below and add this library to your eclipse java project where you will make class to read and write JSON file.
Step 1: To write JSON File
Here, first we will create a JSON file and write some data in it. To do this make a class named "JSONWrite" in java eclipse.
package logicProgramming;
import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class JsonWrite {
public static void main(String[] args) {
JSONObject obj = new JSONObject();
//JSONObject class creates a json object,
//provides a put function to insert the details into json object
obj.put("name", "Abhishek Sharma");
obj.put("department","B.E");
obj.put("branch", "C.S.E");
obj.put("year", 3);
JSONArray list = new JSONArray();
//This is a JSON Array List ,
//It creates an array and then add the values in it
list.add("remark 1");
list.add("remark 2");
list.add("remark 3");
obj.put("remarks", list);//adding the list to our JSON Object
try (FileWriter file = new FileWriter("g:\\newfile.json")) {
//File Writer creates a file in write mode at the given location
file.write(obj.toJSONString());
//write function is use to write in file,
//here we write the Json object in the file
file.flush();
}
catch (IOException e) {
e.printStackTrace();
}
System.out.println(obj);
//to print our JSon object
}
}
This code will create a JSON file named "newfile.json" at the specified location with the following data.
Output
Step 2: To read JSON File
Create a class named "JSONRead" in eclipse. In this we will using "JSONParser" to convert the JSON string in the file to JSONOBject. In order to use JSON Parser makes sure that your string is in JSON format.
package logicProgramming;
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 JsonRead {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
//JsonParser to convert JSON string into Json Object
try {
Object obj = parser.parse(new FileReader("g:\\newfile.json"));
//parsing the JSON string inside the file that we created earlier.
JSONObject jsonObject = (JSONObject) obj;
System.out.println(jsonObject);
//Json string has been converted into JSONObject
String name = (String) jsonObject.get("name");
System.out.println(name);
String department = (String) jsonObject.get("department");
System.out.println(department);
String branch = (String) jsonObject.get("branch");
System.out.println(branch);
long year = (long) jsonObject.get("year");
System.out.println(year);
//Displaying values from JSON OBject by using Keys
JSONArray remarks = (JSONArray) jsonObject.get("remarks");
//converting the JSONObject into JSONArray as remark was an array.
Iterator<String> iterator = remarks.iterator();
//Iterator is used to access the each element in the list
//loop will continue as long as there are elements in the array.
while (iterator.hasNext()) {
System.out.println(iterator.next());
//accessing each elemnt by using next function.
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
This will show you something like this:
Output