Eric Bergman-Terrell's Blog

Javascript Programming Tip: How to Pretty-Print JSON Text
September 11, 2015

This website's JSON Pretty-Print utility uses the following code to convert JSON text into ready-to-read JSON. jsonPrettyPrint first strips all carriage returns and newlines from the original text. The JSON.parse call converts the JSON text to an Object. JSON.stringify then converts that object into an attractively formatted string. I am not aware of any security issues with the JSON.parse call, which is run server-side. If I'm mistaken, please leave a comment!

function jsonPrettyPrint(text) {
    text = JSON.parse(text.replace(/\r|\n/g, ' '));
    text = JSON.stringify(text, null, 4);

    return text;
}
Pretty Print JSON

It's important to be pretty, even if your name isn't Jason!

Keywords: Javascript, JSON, Pretty-Print, Security, JSON, JSON.stringify, JSON.parse, human-readable

Reader Comments

Comment on this Blog Post

Recent Posts

TitleDate
Java Programming Tip: SWT Photo Frame ProgramOctober 31, 2016
Vault 3 (Desktop) Version 1.63 ReleasedSeptember 9, 2016
"Compliance with Court Orders Act of 2016"April 9, 2016
Disable "Visual Voicemail" on Android / T-MobileJanuary 17, 2016
IPv6 HumorDecember 10, 2015
Java Programming Tip: Specify the JVM time zoneDecember 7, 2015
Node.js / Express Programming Tip: Detect and Fix Memory LeaksOctober 27, 2015