How to append data JSON in JAVA

Maestro Vladimir :

Hello i have a json file with data exists like this

[
  {
    "employee": {
      "firstName": "Rudi",
      "lastName": "Anton"
    }
  },
  {
    "employee": {
      "firstName": "Syafira",
      "lastName": "Zarakaria"
    }
  }
]

I want to append json data to this file.

My code now not appending data, re-write file.

Help me

This is my code

    JSONObject employeeDetails = new JSONObject();
    employeeDetails.put("firstName", "Justin");
    employeeDetails.put("lastName", "Saklitinov");

    JSONObject employeeObject = new JSONObject(); 
    employeeObject.put("employee", employeeDetails);


    JSONObject employeeDetails2 = new JSONObject();
    employeeDetails2.put("firstName", "Zara");
    employeeDetails2.put("lastName", "Lovez");

    JSONObject employeeObject2 = new JSONObject(); 
    employeeObject2.put("employee", employeeDetails2);

    //Add employees to list
    JSONArray employeeList = new JSONArray();
    employeeList.add(employeeObject);
    employeeList.add(employeeObject2);

    //Write JSON file
    try{
        String strPath          = "employee.json";
        File strFile = new File(strPath);
        boolean fileCreated = strFile.createNewFile();
       //File appending
        Writer objWriter = new BufferedWriter(new FileWriter(strFile));
        //objWriter.write(employeeList.toJSONString());
        objWriter.flush();
        objWriter.close();
        out.flush();
        out.print("OK");
    } catch (IOException e) {
        e.printStackTrace();
    }
Smile :

You should read the json file into an JSONArray. Add new employee data to this JSONArray and write it to the file. I have modified some of your code.

        // Read json file
        String jsonFileStr = new String(
                Files.readAllBytes(Paths
                        .get("PATH TO YOUR EMPLOYEE JSON")),
                StandardCharsets.UTF_8);
        System.out.println(jsonFileStr);
        JSONArray jsonArray = (JSONArray) new JSONParser().parse(jsonFileStr);

        // Create employee data to be add to json file
        JSONObject employeeDetails = new JSONObject();
        employeeDetails.put("firstName", "Justin");
        employeeDetails.put("lastName", "Saklitinov");

        JSONObject employeeObject = new JSONObject();
        employeeObject.put("employee", employeeDetails);

        JSONObject employeeDetails2 = new JSONObject();
        employeeDetails2.put("firstName", "Zara");
        employeeDetails2.put("lastName", "Lovez");

        JSONObject employeeObject2 = new JSONObject();
        employeeObject2.put("employee", employeeDetails2);

        // Add data to jsonArray read from json file
        jsonArray.put(employeeObject);
        jsonArray.put(employeeObject2);

        // Writing the jsonArray back to file
        String strPath = "PATH TO EMPLOYEE JSON";
        File strFile = new File(strPath);
        boolean fileCreated = strFile.createNewFile();
        Writer objWriter = new BufferedWriter(new FileWriter(strFile));
        objWriter.write(jsonArray.toString());
        objWriter.flush();
        objWriter.close();

Note: You might want to reconsider using the same key 'employee' multiple times in the json.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=421466&siteId=1