Hey there!
Welcome to ClearUrDoubt.com.
In this post, we will look at a Java program to create a JSON file using JSON-simple API.
JSON-simple API is used to create and read JSON files in a simple way.
Maven dependency for JSON-simple:
1 2 3 4 5 |
<dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency> |
Let’s look at the sample program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
package com.clearurdoubt; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.List; import java.util.ArrayList; import org.json.simple.JSONObject; import org.json.simple.JSONArray; /** * @author Sai Gowtham Badvity * Demo program for creating a JSON file using Java * */ public class JSONWriter { @SuppressWarnings("unchecked") public static void main(String[] args) { List<Student> students = new ArrayList<>(); //create sample POJOs students.add(new Student("Rahul", "Kumar", 101, 1)); students.add(new Student("Antony", "James", 102, 0)); students.add(new Student("Ashok", "Kedar", 103, 0)); students.add(new Student("Ravi", "Aswin", 104, 1)); JSONArray studentArray = new JSONArray(); try { File outputFile = new File("output.json"); FileWriter writer = new FileWriter(outputFile); //loop through all the POJOs students.forEach( s -> { JSONObject studentJSON = new JSONObject(); //create a JSON object for each POJO studentJSON.put("first_name", s.first_name); studentJSON.put("last_name", s.last_name); studentJSON.put("id", s.id); studentJSON.put("student_type", s.student_type); //add the JSON object to JSONArray studentArray.add(studentJSON); }); System.out.println(studentArray.toJSONString()); writer.write(studentArray.toJSONString()); //write the JSON string to the file for creating a JSON file writer.flush(); writer.close(); System.out.println("JSON File is created."); } catch(IOException ioe) { System.out.println("IOException occured. " + ioe.getMessage()); ioe.printStackTrace(); } catch(Exception ex) { System.out.println("An Exception occured. " + ex.getMessage()); ex.printStackTrace(); } } } class Student { String first_name; String last_name; int id; int student_type; Student(String first_name, String last_name, int id, int student_type) { this.first_name = first_name; this.last_name = last_name; this.id = id; this.student_type = student_type; } public String getFirst_name() { return first_name; } public void setFirst_name(String first_name) { this.first_name = first_name; } public String getLast_name() { return last_name; } public void setLast_name(String last_name) { this.last_name = last_name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getStudent_type() { return student_type; } public void setStudent_type(int student_type) { this.student_type = student_type; } } |
Output file(represented in JSON format for readability):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
[{ "last_name": "Kumar", "id": 101, "first_name": "Rahul", "student_type": 1 }, { "last_name": "James", "id": 102, "first_name": "Antony", "student_type": 0 }, { "last_name": "Kedar", "id": 103, "first_name": "Ashok", "student_type": 0 }, { "last_name": "Aswin", "id": 104, "first_name": "Ravi", "student_type": 1 }] |
Happy Learning :).
Please leave a reply in case of any queries.