Pretty-Print JSON in Java

mabuzer picture mabuzer · Nov 5, 2010 · Viewed 247k times · Source

I'm using and I need to pretty-print JSON data (make it more human readable).

I haven't been able to find this functionality within that library. How is this commonly achieved?

Answer

Ray Hulha picture Ray Hulha · Sep 5, 2011

GSON can do this in a nice way:

Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(uglyJSONString);
String prettyJsonString = gson.toJson(je);