-3

I am developing a java console application and using JSON to store data. This is my query. I want to add data inside the existing node without losing data.

I have this Node:

{
  "course" :
    [

    ],
}

Now I want to write new data inside course, for example:

{
  "course" :
    [
      "coursel", {
        "EndDate" : "16/09/2022",
        "StartDate" : "16/09/2022"
       },
       "course2", {
        "EndDate" : "1/10/2022",
        "StartDate" : "1/10/2021"
       }
    ],
}
I_love_vegetables
  • 1,575
  • 5
  • 12
  • 26
Hannan Max
  • 49
  • 6

2 Answers2

1

Please avoid pasting code snippet as image in your question. Looks like the json structure you mentioned is invalid. Please refer below code.

Actual Json (Before running the program):

{
  "course": [
    {
      "course": "course1",
      "StartDate": "16/09/2021",
      "EndDate": "16/09/2022"
    },
    {
      "course": "course2",
      "StartDate": "01/10/2021",
      "EndDate": "01/10/2022"
    }
  ]
}

Source Code:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

public class SO2 {


    public static void main(String[] args) {

        List<CourseDetail> courseList = new ArrayList<>();
        courseList.add(new CourseDetail("course3", "09/09/2021", "09/09/2022"));

        writeCourseList(courseList, "/Users/MyiCloud/Documents/Java/MavenOfflineMode/json", "courses.json");
    }

    public static void writeCourseList(List<CourseDetail> courseList, String path, String fileName) {
        Course course = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            byte[] jsonData = Files.readAllBytes(Paths.get(path + File.separator + fileName));
            course = objectMapper.readValue(jsonData, Course.class);

            List<CourseDetail> existingCourseList = course.getCourseList();
            if(null != existingCourseList && existingCourseList.size() > 0) {
                courseList.forEach(newCourse -> existingCourseList.add(newCourse));
                course.setCourseList(existingCourseList);
            } else {
                course.setCourseList(courseList);
            }
            objectMapper.writeValue(new File(path + File.separator + fileName), course);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class Course {
    @JsonProperty("course")
    private List<CourseDetail> courseList = new ArrayList<>();

    public List<CourseDetail> getCourseList() {
        return courseList;
    }

    public void setCourseList(List<CourseDetail> courseList) {
        this.courseList = courseList;
    }
}

class CourseDetail {

    @JsonProperty("course")
    private String courseName;
    @JsonProperty("StartDate")
    private String startDate;
    @JsonProperty("EndDate")
    private String endDate;


    public CourseDetail() {
    }

    public CourseDetail(String courseName, String startDate, String endDate) {
        this.courseName = courseName;
        this.startDate = startDate;
        this.endDate = endDate;
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

}

Json (After running the code):

{
  "course": [
    {
      "course": "course1",
      "StartDate": "16/09/2021",
      "EndDate": "16/09/2022"
    },
    {
      "course": "course2",
      "StartDate": "01/10/2021",
      "EndDate": "01/10/2022"
    },
    {
      "course": "course3",
      "StartDate": "09/09/2021",
      "EndDate": "09/09/2022"
    }
  ]
}
Vasanth Subramanian
  • 1,040
  • 1
  • 13
  • 32
0

you could do something like below using JacksonAPI,

toJson = objectMapper.readTree(objectMapper.writeValueAsString(from));

Add course object toJson and save it.

I_love_vegetables
  • 1,575
  • 5
  • 12
  • 26