Java String just becomes JSON String value, that is, surrounded in double
quotes and contents escaped if and as necessary.

So to pretty-print, you need to parse JSON into a Java object structure,
then write with pretty-printing. Something like:

JsonNode root = mapper.readTree(input);
String pretty =
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root);

Hope this helps!

-+ Tatu +-



On Thu, Oct 13, 2016 at 11:45 PM, Serdyn du Toit <dutoi...@gmail.com> wrote:

> Hi,
>
> I'm having a problem getting a String of Json to "pretty print".  After
> doing some cleanup of the String I can get it "pretty printed" using
> https://jsonformatter.curiousconcept.com/, so I think I'm just using the
> Jackson Api in the wrong way.
>
>
> Here is my code:
> @Test
> public void testToPrettyJson_ProblematicString() throws Exception {
> String problematicString = "{\"data\":[{\"id\":\"
> 57fa47de76a073d78a63b726\",\"firstName\":\"pVvnTRAv\",\"
> lastName\":\"ABtWQaSW\",\"fullName\":\"ABtWQaSW,
> pVvnTRAv\",\"emailAddress\":\"20161009-15363...@test.com\",\
> "workNumber\":\"+27 12 123 1234\",\"cellNumber\":null,\"
> homeNumber\":null,\"status\":\"Pending approval\",\"displayStatus\":\"Pending
> approval\",\"displayStatusColor\":\"<span style=\\\"color:red\\\">Pending
> approval</span>\",\"suspensionNotes\":\"<span
> style=\\\"color:red\\\">Test suspension notes.</span>\",\"notes\":
> null,\"createDate\":\"09/10/2016\",\"lastUpdate\":\"09/10/
> 2016\",\"admin\":\"System\",\"isStatusActive\":false}],\"
> recordsTotal\":0,\"recordsFiltered\":521,\"draw\":1}";
> trace("Problematic string: " + problematicString);
> problematicString = problematicString.replaceAll("\\\"", "\"");
> trace("Problematic string after manual cleanup: " + problematicString);
> String json = new ObjectMapper().writerWithDefaultPrettyPrinter
> ().writeValueAsString(problematicString);
> trace("Json: " + json);
> }
>
>
> So I think the problem is that I'm using #writeValueAsString(..) when I'm
> already working with a String.  How can I get my String to "pretty print"?
> When I work with objects the #writeValueAsString(..) method works fine, so
> I'm just unsure how to proceed now that I'm working with a String...
>
>
> Here's the test's output:
>
> Problematic string: {"data":[{"id":"57fa47de76a073d78a63b726","
> firstName":"pVvnTRAv","lastName":"ABtWQaSW","fullName":"ABtWQaSW,
> pVvnTRAv","emailAddress":"20161009-15363...@test.com","workNumber":"+27
> 12 123 1234","cellNumber":null,"homeNumber":null,"status":"Pending
> approval","displayStatus":"Pending approval","displayStatusColor":"<span
> style=\"color:red\">Pending approval</span>","suspensionNotes":"<span
> style=\"color:red\">Test suspension notes.</span>","notes":null,"
> createDate":"09/10/2016","lastUpdate":"09/10/2016","admin":"System","
> isStatusActive":false}],"recordsTotal":0,"recordsFiltered":521,"draw":1}
>
> Problematic string after manual cleanup: {"data":[{"id":"
> 57fa47de76a073d78a63b726","firstName":"pVvnTRAv","lastName":"ABtWQaSW","fullName":"ABtWQaSW,
> pVvnTRAv","emailAddress":"20161009-15363...@test.com","workNumber":"+27
> 12 123 1234","cellNumber":null,"homeNumber":null,"status":"Pending
> approval","displayStatus":"Pending approval","displayStatusColor":"<span
> style=\"color:red\">Pending approval</span>","suspensionNotes":"<span
> style=\"color:red\">Test suspension notes.</span>","notes":null,"
> createDate":"09/10/2016","lastUpdate":"09/10/2016","admin":"System","
> isStatusActive":false}],"recordsTotal":0,"recordsFiltered":521,"draw":1}
>
> Json: "{\"data\":[{\"id\":\"57fa47de76a073d78a63b726\",\"
> firstName\":\"pVvnTRAv\",\"lastName\":\"ABtWQaSW\",\"fullName\":\"ABtWQaSW,
> pVvnTRAv\",\"emailAddress\":\"20161009-15363...@test.com\",\
> "workNumber\":\"+27 12 123 1234\",\"cellNumber\":null,\"
> homeNumber\":null,\"status\":\"Pending approval\",\"displayStatus\":\"Pending
> approval\",\"displayStatusColor\":\"<span style=\\\"color:red\\\">Pending
> approval</span>\",\"suspensionNotes\":\"<span
> style=\\\"color:red\\\">Test suspension notes.</span>\",\"notes\":
> null,\"createDate\":\"09/10/2016\",\"lastUpdate\":\"09/10/
> 2016\",\"admin\":\"System\",\"isStatusActive\":false}],\"
> recordsTotal\":0,\"recordsFiltered\":521,\"draw\":1}"
>
>
> Any insight appreciated!  Many thanks!!! :)
>
> --
> You received this message because you are subscribed to the Google Groups
> "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jackson-user+unsubscr...@googlegroups.com.
> To post to this group, send email to jackson-user@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jackson-user+unsubscr...@googlegroups.com.
To post to this group, send email to jackson-user@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to