Skip to main content
Add link to the mentioned answer
Source Link
Benjamin Loison
  • 5.8k
  • 4
  • 20
  • 37

Here's user123444555621's awesome HTML oneuser123444555621's awesome HTML one adapted for terminals. Handy for debugging Node scripts:

function prettyJ(json) { if (typeof json !== 'string') { json = JSON.stringify(json, undefined, 2); } return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) { let cls = "\x1b[36m"; if (/^"/.test(match)) { if (/:$/.test(match)) { cls = "\x1b[34m"; } else { cls = "\x1b[32m"; } } else if (/true|false/.test(match)) { cls = "\x1b[35m"; } else if (/null/.test(match)) { cls = "\x1b[31m"; } return cls + match + "\x1b[0m"; } ); } 

Usage:

// thing = any json OR string of json prettyJ(thing); 

Here's user123444555621's awesome HTML one adapted for terminals. Handy for debugging Node scripts:

function prettyJ(json) { if (typeof json !== 'string') { json = JSON.stringify(json, undefined, 2); } return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) { let cls = "\x1b[36m"; if (/^"/.test(match)) { if (/:$/.test(match)) { cls = "\x1b[34m"; } else { cls = "\x1b[32m"; } } else if (/true|false/.test(match)) { cls = "\x1b[35m"; } else if (/null/.test(match)) { cls = "\x1b[31m"; } return cls + match + "\x1b[0m"; } ); } 

Usage:

// thing = any json OR string of json prettyJ(thing); 

Here's user123444555621's awesome HTML one adapted for terminals. Handy for debugging Node scripts:

function prettyJ(json) { if (typeof json !== 'string') { json = JSON.stringify(json, undefined, 2); } return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) { let cls = "\x1b[36m"; if (/^"/.test(match)) { if (/:$/.test(match)) { cls = "\x1b[34m"; } else { cls = "\x1b[32m"; } } else if (/true|false/.test(match)) { cls = "\x1b[35m"; } else if (/null/.test(match)) { cls = "\x1b[31m"; } return cls + match + "\x1b[0m"; } ); } 

Usage:

// thing = any json OR string of json prettyJ(thing); 
Source Link

Here's user123444555621's awesome HTML one adapted for terminals. Handy for debugging Node scripts:

function prettyJ(json) { if (typeof json !== 'string') { json = JSON.stringify(json, undefined, 2); } return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) { let cls = "\x1b[36m"; if (/^"/.test(match)) { if (/:$/.test(match)) { cls = "\x1b[34m"; } else { cls = "\x1b[32m"; } } else if (/true|false/.test(match)) { cls = "\x1b[35m"; } else if (/null/.test(match)) { cls = "\x1b[31m"; } return cls + match + "\x1b[0m"; } ); } 

Usage:

// thing = any json OR string of json prettyJ(thing);