12

I have data in a Map object and I want to print it in a json format. I tried using DefaultPrettyPrinter

mapper.writerWithDefaultPrettyPrinter().writeValue(filePath, mapObject);

but the format is not what I expected. I am getting output like this:

{
  "arrVals" : ["value-1","value-2"]
}

I want output like this:

{
  "arrVals" : [
    "value-1",
    "value-2"
  ]
}
Manisha
  • 123
  • 1
  • 7
  • There is a simmilar question here: http://stackoverflow.com/questions/6176881/how-do-i-make-jacksons-build-method-pretty-print-its-json-output (NOT a duplicate in my opinion) that can help you – morgano Aug 07 '13 at 08:48
  • I have seen that but it doesn't solve the problem. I need printing array values in next line. – Manisha Aug 07 '13 at 08:50
  • 1
    Similar question: http://stackoverflow.com/questions/17411586/jackson-json-not-formatting-correctly/17415099 – Michał Ziober Aug 07 '13 at 09:21

1 Answers1

21

You need indentation before Array Values. You can use indentArraysWith method to set the Lf2SpacesIdenter object which will basically add a line feed followed by 2 spaces. This might solve your problem.

DefaultPrettyPrinter pp = new DefaultPrettyPrinter();
pp.indentArraysWith(new Lf2SpacesIndenter());
mapper.writer(pp).writeValue(filePath, mapObject);
Mady
  • 5,016
  • 7
  • 35
  • 46
  • 8
    Lf2SpacesIndenter is at DefaultPrettyPrinter.Lf2SpacesIndenter The constructor and instance field are deprecated. Use: pp.indentArraysWith( DefaultIndenter.SYSTEM_LINEFEED_INSTANCE ); – djb Feb 17 '16 at 03:52