Writing JSON file with pretty print
Asked Answered
S

1

6

In the following code we write objects and an array of type JSON to a text file:

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {


    JSONObject obj = new JSONObject();
    obj.put("Name", "crunchify.com");
    obj.put("Author", "App Shah");

    JSONArray company = new JSONArray();
    company.add("Compnay: eBay");
    company.add("Compnay: Paypal");
    company.add("Compnay: Google");
    obj.put("Company List", company);

    // try-with-resources statement based on post comment below :)
    try (FileWriter file = new FileWriter("file.txt")) {


                    Gson gson = new GsonBuilder().setPrettyPrinting().create();
                    JsonParser jp = new JsonParser();
                    JsonElement je = jp.parse(obj.toJSONString());
                    String prettyJsonString = gson.toJson(je);
                    System.out.println(prettyJsonString);                  

                    file.write(prettyJsonString);
        System.out.println("Successfully Copied JSON Object to File...");
        System.out.println("\nJSON Object: " + obj);

                    file.flush();
                    file.close();
    }


}

}

in the following code we pretty print JSONtostring:

                    Gson gson = new GsonBuilder().setPrettyPrinting().create();
                    JsonParser jp = new JsonParser();
                    JsonElement je = jp.parse(obj.toJSONString());
                    String prettyJsonString = gson.toJson(je);
                    System.out.println(prettyJsonString);                  

Print result of prettyJsonString is:

{
      "Name": "crunchify.com",
      "Author": "App Shah",
       "Company List": [
      "Compnay: eBay",
       "Compnay: Paypal",
       "Compnay: Google"
    ]
    }

But when we write prettyJsonString to file, the result is linear and does not look like the result above.

file.write(prettyJsonString);

{  "Name": "crunchify.com",  "Author": "App Shah",  "Company List": [    "Compnay: eBay",    "Compnay: Paypal",    "Compnay: Google"  ]}

How can we write to file and make the result nice and pretty like the System.out.prinln of prettyJsonString above?? Thanks allot

Sporozoite answered 18/11, 2015 at 16:36 Comment(3)
mkyong.com/java/how-to-enable-pretty-print-json-output-gsonIschia
How are you viewing the file? Notepad is notorious for stripping new lines. Do you see the newlines stripped when you see with a editor like Notepad++ (or even WordPad)?Halloo
Yes using notepad++ the newlines are not stripped and the JSON is in pretty format. thanksSporozoite
D
0

As Nivas said in the comments, some programs strip newlines, so viewing the output in those programs (such as Notepad) could make them look "ugly". Make sure you are viewing them in a program that displays newlines correctly, such as Notepad++.

Dogcart answered 18/11, 2015 at 16:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.