Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Beautifier Library for Java

I want to format a string containing JSON data using Java. Does anybody know an open source library for that.

like image 966
baranco Avatar asked Mar 28 '11 10:03

baranco


1 Answers

Assuming you're starting out with an existing JSON string, then Jackson can do this for you:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);

String originalJson = ...
JsonNode tree = objectMapper .readTree(originalJson);
String formattedJson = objectMapper.writeValueAsString(tree);
like image 149
skaffman Avatar answered Oct 02 '22 19:10

skaffman