JAVA code to convert a JSON String to CSV or Excel

  • The JSON extends to Javascript Object Notion.
  • It is used for transferring data between client and server applications.
  • It is used to encapsulate data in a readable and organized format.
  • JSON is mostly used in REST APIs.

Now for making JSON readable in excel or csv below is the JAVA code.

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.json.*;
public class JSONtoCSV{
   public static void main(String[] args) {
      String JSONformat = "{\"Data\": [{\"Employee Name\": \"Tobo\",\"Work\": \"Writer\",\"Location\": \"Noida\"}]}";
      JSONObject Obj1;
      try {
         Obj1 = new JSONObject(JSONformat);
         JSONArray arr1 = Obj1.getJSONArray("Data");
         File file = new File("C:\\Tobo\\EmployeeData.csv");
         String table = CDL.toString(arr1);
         FileUtils.writeStringToFile(file, table);
         System.out.println("CSV written to "+ file);
         System.out.println(csv);
      }
      catch(Exception e) {
	System.out.println("JSON format is invalid");
         e.printStackTrace();
      }
   }
}

O/P

CSV written to EmpDetails.csv
Employee name,Work,Location
Tobo,Writer,Noida

Happy CODING 🙂

TOBO’s POINT

4 thoughts on “JAVA code to convert a JSON String to CSV or Excel

  1. Great goods from you, man. I have understand
    your stuff previous to and you’re just extremely excellent.
    I actually like what you have acquired here, really like what you’re stating and the way in which you
    say it. You make it enjoyable and you still take care of to keep it wise.
    I can’t wait to read much more from you. This is really a
    terrific site.

  2. wonderful submit, very informative. I wonder why the other specialists of this
    sector do not realize this. You must proceed your writing.
    I am confident, you’ve a huge readers’ base already!

  3. Good post. I learn something totally new and challenging on blogs I stumbleupon on a daily basis.
    It’s always exciting to read content from other writers and use something from their sites.

Leave a Reply to web hosting are Cancel reply

Your email address will not be published.

Verified by MonsterInsights