Membuat JSON di Java

saat postingan ini dibuat untuk membuat objek JSON di java kita perlu menggunakan library yang paling populer yaitu Jackson

Hasilnya akan seperti berikut:

        {
         "students" : [ {
           "idCard" : "2019110024",
           "name" : "Muhammat Rizki Saputra",
           "major" : "Teknik Informatika",
           "hobbies" : [ "coding", "sport" ]
         }, {
           "idCard" : "2019110025",
           "name" : "Muhammat Sarif",
           "major" : "Teknik Informatika",
           "hobbies" : [ "coding", "sport", "music" ]
         }, {
           "idCard" : "2019110026",
           "name" : "Muhammat Alamsyah",
           "major" : "Teknik Informatika",
           "hobbies" : [ "coding" ]
         }, {
           "idCard" : "2019110027",
           "name" : "Muhammat Alim",
           "major" : "Teknik Informatika",
           "hobbies" : [ "aventure" ]
         }, {
           "idCard" : "2019110028",
           "name" : "Muhammat Rafly Al ajid",
           "major" : "Teknik Informatika",
           "hobbies" : [ "sport" ]
         } ]
       }

Tambahkan library jackson kedalam depedency:

		<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.15.2</version>
    </dependency>

ada dua cara membuat objek JSON di java

Membuat JSON dari Collection MAP

        
       @Data
       @NoArgsConstructor
       @AllArgsConstructor
       public class Students {
           private String idCard;
           private String name;
           private String major;
           private List<String> hobbies;
       }
            
           void createJsonFromMap() throws IOException {
               List<Students> studentsList = List.of(
                       new Students("2019110024", "Muhammat Rizki Saputra", "Teknik Informatika", List.of("coding", "sport")),
                       new Students("2019110025", "Muhammat Sarif", "Teknik Informatika", List.of("coding", "sport", "music")),
                       new Students("2019110026", "Muhammat Alamsyah", "Teknik Informatika", List.of("coding")),
                       new Students("2019110027", "Muhammat Alim", "Teknik Informatika", List.of("aventure")),
                       new Students("2019110028", "Muhammat Rafly Al ajid", "Teknik Informatika", List.of("sport"))
               );
               Map<String, List<Students>> students = Map.of(
                   "students", studentsList
               );
       
               ObjectMapper mapper = new ObjectMapper()
                       .enable(SerializationFeature.INDENT_OUTPUT);
       
               mapper.writer()
                       .writeValue(Files.newBufferedWriter(Path.of("students.json")), students);
           }
       

Membuat JSON dari Objek Java

            
           void createJsonFromObject() throws IOException {
               List<Students> studentsList = List.of(
                       new Students("2019110024", "Muhammat Rizki Saputra", "Teknik Informatika", List.of("coding", "sport")),
                       new Students("2019110025", "Muhammat Sarif", "Teknik Informatika", List.of("coding", "sport", "music")),
                       new Students("2019110026", "Muhammat Alamsyah", "Teknik Informatika", List.of("coding")),
                       new Students("2019110027", "Muhammat Alim", "Teknik Informatika", List.of("aventure")),
                       new Students("2019110028", "Muhammat Rafly Al ajid", "Teknik Informatika", List.of("sport"))
               );
               StudentsJSON studentsJSON = new StudentsJSON(studentsList);
       
               ObjectMapper mapper = new ObjectMapper()
                       .enable(SerializationFeature.INDENT_OUTPUT)
                       .setSerializationInclusion(JsonInclude.Include.NON_NULL);
       
               mapper.writer()
                       .writeValue(Files.newBufferedWriter(Path.of("students.json")), studentsJSON);
           }
            
           @Data
           @NoArgsConstructor
           @AllArgsConstructor
           public class StudentsJSON {
               private List<Students> students;
           }
           
           
           @Data
           @NoArgsConstructor
           @AllArgsConstructor
           public class Students {
               private String idCard;
               private String name;
               private String major;
               private List<String> hobbies;
           }