Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / pretty-print JSON using JavaScript

pretty-print JSON using JavaScript

August 1, 2021 by James Palmer

Pretty-printing is implemented natively in JSON.stringify(). The third argument enables pretty printing and sets the spacing to use:
var str = JSON.stringify(obj, null, 2); // spacing level = 2

If you need syntax highlighting, you might use some regex magic like so:
function syntaxHighlight(json) {
if (typeof json != ‘string’) {
json = JSON.stringify(json, undefined, 2);
}
json = json.replace(/&/g, ‘&’).replace(//g, ‘>’);
return json.replace(/(“(\u[a-zA-Z0-9]{4}|\[^u]|[^\”])*”(s*:)?|b(true|false|null)b|-?d+(?:.d*)?(?:[eE][+-]?d+)?)/g, function (match) {
var cls = ‘number’;
if (/^”/.test(match)) {
if (/:$/.test(match)) {
cls = ‘key’;
} else {
cls = ‘string’;
}
} else if (/true|false/.test(match)) {
cls = ‘boolean’;
} else if (/null/.test(match)) {
cls = ‘null’;
}
return ‘‘ + match + ‘‘;
});
}

See in action here: jsfiddle
Or a full snippet provided below:

function output(inp) {

document.body.appendChild(document.createElement(‘pre’)).innerHTML = inp;

}

function syntaxHighlight(json) {

json = json.replace(/&/g, ‘&’).replace(//g, ‘>’);

return json.replace(/(“(\u[a-zA-Z0-9]{4}|\[^u]|[^\”])*”(s*:)?|b(true|false|null)b|-?d+(?:.d*)?(?:[eE][+-]?d+)?)/g, function (match) {

var cls = ‘number’;

if (/^”/.test(match)) {

if (/:$/.test(match)) {

cls = ‘key’;

} else {

cls = ‘string’;

}

} else if (/true|false/.test(match)) {

cls = ‘boolean’;

} else if (/null/.test(match)) {

cls = ‘null’;

}

return ‘‘ + match + ‘‘;

});

}

var obj = {a:1, ‘b’:’foo’, c:[false,’false’,null, ‘null’, {d:{e:1.3e5,f:’1.3e5′}}]};

var str = JSON.stringify(obj, undefined, 4);

output(str);

output(syntaxHighlight(str));
pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }

.string { color: green; }

.number { color: darkorange; }

.boolean { color: blue; }

.null { color: magenta; }

.key { color: red; }

User Pumbaa80’s answer is great if you have an object you want pretty printed. If you’re starting from a valid JSON string that you want to pretty printed, you need to convert it to an object first:
var jsonString = ‘{“some”:”json”}’;
var jsonPretty = JSON.stringify(JSON.parse(jsonString),null,2);

This builds a JSON object from the string, and then converts it back to a string using JSON stringify’s pretty print.

Related

Filed Under: Uncategorized

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2025 · News Pro Theme on Genesis Framework · WordPress · Log in