JSON Beautifier Library for Java

With Jackson 2.6.1

String beautify(String json) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    Object obj = mapper.readValue(json, Object.class);
    return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
}

pom.xml:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.6.1</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.1</version>
    </dependency>

Convert JSON String to Pretty Print JSON output using Jackson


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);

Update to previous answer by skaffman, with newer versions of Jackson (2+, I think). The second line of code is now:

objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);