Write JSON object in a file

    0

    12

    jsonwritefile
    Java Public Recipes

    Write a JSON object directly in a file

    import java.io.FileWriter;
    import org.json.simple.JSONArray;
    import org.json.simple.JSONObject;
    
    try{
      JSONObject myJsonObject = new JSONObject(); 
      FileWriter file = new FileWriter("/path/to/file/to/write.json");
      myJsonObject.put("key", "value");
      file.write(myJsonObject.toJSONString()); 
      file.flush();
    } catch (IOException e) {
      e.printStackTrace();
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.