Tuesday, August 20, 2019

How to Write & Read JSON file using Java



JSON:
    JSON (an acronym for JavaScript Object Notation) is a lightweight data-interchange format and is most commonly used for client-server communication. It’s both easy to read/write and language independent. A JSON value can be another JSON object, array, number, string, Boolean (true/false) or null.

Add Json.simple maven dependency to your pom.xml file:
<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>


              
 Writing Data to JSON file: 
package practise;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

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

public class JSONWriteExample {

   public static void main( String[] args ) throws IOException
    {
        //First user
        JSONObject employeeDetails = new JSONObject();
        employeeDetails.put("firstName", "Mahantesh");
        employeeDetails.put("lastName", "Hadimani");
        employeeDetails.put("website", "mahantesh-hadimani.blogspot.com.com");
        
        JSONObject employeeObject = new JSONObject();
        employeeObject.put("user", employeeDetails);
        
        //Second user
        JSONObject employeeDetails2 = new JSONObject();
        employeeDetails2.put("firstName", "abc");
        employeeDetails2.put("lastName", "xyz");
        employeeDetails2.put("website", "google.com");
        
        JSONObject employeeObject2 = new JSONObject();
        employeeObject2.put("user", employeeDetails2);
        
        //Add user to list
        JSONArray employeeList = new JSONArray();
        employeeList.add(employeeObject);
        employeeList.add(employeeObject2);
        
        //Write JSON file
        File file = new File("userDetails.json");
        file.createNewFile();
        FileWriter fileWriter = new FileWriter(file);
        System.out.println("Writing JSON object to file"); 
        System.out.println("-----------------------"); 
        fileWriter.write(employeeList.toJSONString());
        fileWriter.flush();

       
    }
}




Output:
[
   {
      "user":
                {
                   "firstName":"Mahantesh",
                   "lastName":"Hadimani",
                   "website":"mahantesh-hadimani.blogspot.com.com"
                }
   },

   {
        "user":
                {
                   "firstName":"abc",
                   "lastName":"xyz",
                   "website":"google.com"
                 }
   }
]

Read data from Json file :
package practise;

import java.io.FileReader;
import java.io.IOException;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class ReadJSONExample {

   public static void main(String[] args) throws IOException, ParseException
    {
        //JSON parser object to parse read file
        JSONParser jsonParser = new JSONParser();
        
        FileReader reader = new FileReader("userDetails.json");
       
        //Read JSON file
         Object obj = jsonParser.parse(reader);
          System.out.println(obj);
     }
}





Output:
[

   {
      "user":
                {
                   "firstName":"Mahantesh",
                   "lastName":"Hadimani",
                   "website":"mahantesh-hadimani.blogspot.com.com"
                }
   },

   {
        "user":
                {
                   "firstName":"abc",
                   "lastName":"xyz",
                   "website":"google.com"
                 }
   }
]





No comments:

Post a Comment

If any suggestions or issue, please provide