How do I make Jackson's build() method pretty-print its JSON output? Here is an example that pretty-prints the ugly form of JSON code. I need to take the nice version of
The safe way is to read the data using the "Raw" data binding and just write it out again without enabling the pretty printer. The Streaming API is probably your friend here.
Using a regexp does also work if you use this pattern: \s*\n\s*
This doesn't create the most compact form (i.e. you will still have some spaces between elements) but it's a cheap solution if you already have the JSON as a String. The reason why this pattern is safe is that new lines are invalid in String values (they must be escaped using \n
) so you can safely remove whitespace around them.