Skip to content
Advertisement

Convert JSON String to Pretty Print JSON output using Jackson

This is the JSON string I have:

JavaScript

I need to convert the above JSON String into Pretty Print JSON Output (using Jackson), like below:

JavaScript

Can anyone provide me an example based on my example above? How to achieve this scenario? I know there are lot of examples, but I am not able to understand those properly. Any help will be appreciated with a simple example.

Updated:

Below is the code I am using:

JavaScript

But this doesn’t works with the way I needed the output as mentioned above.

Here’s is the POJO I am using for the above JSON:

JavaScript

Can anyone tell me whether I got the right POJO for the JSON or not?

Updated:

JavaScript

Below line prints out something like this:

JavaScript

which is the way I needed to be shown. But when I add it to model like this:

JavaScript

And then shows it out in a resultform jsp page like below:

JavaScript

I get something like this:

JavaScript

which I don’t need. I needed the way it got printed out above. Can anyone tell me why it happened this way?

Advertisement

Answer

To indent any old JSON, just bind it as Object, like:

JavaScript

and then write it out with indentation:

JavaScript

this avoids your having to define actual POJO to map data to.

Or you can use JsonNode (JSON Tree) as well.

Advertisement