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
.NET Public-Key (Asymmetric) Cryptography DemoJuly 20, 2025
Raspberry Pi 3B+ Photo FrameJune 17, 2025
EBTCalc (Android) Version 1.53 is now availableMay 19, 2024
Vault 3 Security EnhancementsOctober 24, 2023
Vault 3 is now available for Apple OSX M2 Mac Computers!September 18, 2023
Vault (for Desktop) Version 0.77 ReleasedMarch 26, 2023
EBTCalc (Android) Version 1.44 is now availableOctober 12, 2021