Jackson convert json to pretty json 2017-03-09 05:29

I have showed you how to convert object to pretty json in my previous blog. Today I will how you how to convert json string to pretty json. Before using writerWithDefaultPrettyPrinter method we need to convert json string to Object first then convert object to pretty json.

Use Jackson to convert string json to pretty json. The pom file is like following.




public class StringPrettyOutput {
    public static void main(String[] args) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        String str = "{ \"name\" : \"HenryXi\", \"age\" : 28 }";
        Object json = objectMapper.readValue(str, Object.class);// convert json to Object 

The output is like following

  "name" : "HenryXi",
  "age" : 28