Pretty Print and Format JSON
Pretty Print and Format JSON is a reusable JavaScript snippet. Formats JSON strings or objects for human-readable display with syntax highlighting support. The full implementation: function prettyPrint(data, indent = 2) { return JSON.stringify(data, null, indent) } // Colorized for console function prettyPrintColor(data) { const json = JSON.stringify(data, null, 2) return json .replace(/("(?:\.|[^"])*")s*:/g, '\x1b[36m$1\x1b[0m:') // keys: cyan .replace(/: "((?:\.|[^"])*)"/g, ': \x1b[32m"$1"\x1b[0m') // strings: green .replace(/: (\d+\.?\d*)/g, ': \x1b[33m$1\x1b[0m') // numbers: yellow .replace(/: (true|false|null)/g, ': \x1b[35m$1\x1b[0m') // booleans/null: magenta } // Size-friendly JSON (strip whitespace) function minify(data) { return JSON.stringify(data) } // Usage prettyPrint({ a: 1, b: { c: [1, 2, 3] } }) // { //… This snippet uses only standard JavaScript with no dependencies, so it runs in modern browsers and Node.js alike. Browse more patterns in the snippet library, or paste the code straight into the playground to experiment with it.