We are saving the REST request and response in JSON file and saving that .JSON file into a folder. We verified the output file, it is displaying the entire response in a single row. We have lots of JSON files need to be formatted to display in JSON format(readable format) only. I need java code to do this. If anybody already worked on this kind of issue and how the issue get resolved please let me know. Thanks
1 Answers
If you are using java,then do like this. Get into that folder. List all the file names and iterate in a for loop. Inside that for loop, get contents from the file and parse it, clear the previous contents from that file, then insert the parsed content it into that file.
Done..
To parse in readable format, follow the bellow steps, choose the convenient method.
Try using gson
Gson gson = new GsonBuilder().setPrettyPrinting().create(); JsonParser jp = new JsonParser(); JsonElement je = jp.parse(uglyJSONString); String prettyJsonString = gson.toJson(je);
Or try this
JSONObject json = new JSONObject(jsonString); // Convert text to object System.out.println(json.toString(4)); // Print it with specified indentation
Or this
Gson gson = new GsonBuilder().setPrettyPrinting().create(); String jsonOutput = gson.toJson(someObject);
Or this
ObjectMapper mapper = new ObjectMapper(); System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject))
Or this

- 220
- 3
- 19