Search code examples
javajsonformattingpretty-print

How do I pretty-print existing JSON data with Java?


I have a compact JSON string, and I want to format it nicely in Java without having to deserialize it first -- e.g. just like jsonlint.org does it. How could I do this via code? If libraries are needed, please provide relevant info.

A similar solution for XML would also be nice.


Solution

  • I think for pretty-printing something, it's very helpful to know its structure.

    To get the structure you have to parse it. Because of this, I don't think it gets much easier than first parsing the JSON string you have and then using the pretty-printing method toString mentioned in the comments above.

    Of course you can do similar with any JSON library you like.