Please use a <pre> tag
demo : http://jsfiddle.net/K83cK/
var data = {
"data": {
"x": "1",
"y": "1",
"url": "http://url.com"
},
"event": "start",
"show": 1,
"id": 50
}
document.getElementById("json").textContent = JSON.stringify(data, undefined, 2);
<pre id="json"></pre>
Answer from Diode on Stack Overflow Top answer 1 of 15
745
Please use a <pre> tag
demo : http://jsfiddle.net/K83cK/
var data = {
"data": {
"x": "1",
"y": "1",
"url": "http://url.com"
},
"event": "start",
"show": 1,
"id": 50
}
document.getElementById("json").textContent = JSON.stringify(data, undefined, 2);
<pre id="json"></pre>
2 of 15
45
Make sure the JSON output is in a <pre> tag.
Videos
How to Format & Display a Json in HTML + Javascript - YouTube
02:38
JavaScript tips — Pretty print json strings with JSON.stringify ...
20:35
Create Your Own JSON Formatter with JavaScript (Prettify/Minify) ...
How to Pretty Print JSON | React Tutorial
04:55
JavaScript Pretty Print JSON Object - YouTube
npm
npmjs.com › package › pretty-print-json
pretty-print-json - npm
February 27, 2026 - Latest version: 3.0.7, last published: a month ago. Start using pretty-print-json in your project by running `npm i pretty-print-json`. There are 13 other projects in the npm registry using pretty-print-json.
» npm install pretty-print-json
ReqBin
reqbin.com › code › javascript › ounkkzpp › javascript-pretty-print-json-example
How do I pretty print JSON in JavaScript?
JavaScript has a built-in JSON.stringify(obj, replacer, space) method to convert objects to JSON and pretty-print the generated JSON string.
Top answer 1 of 16
6933
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, '<').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 '<span class="' + cls + '">' + match + '</span>';
});
}
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, '<').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 '<span class="' + cls + '">' + match + '</span>';
});
}
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; }
2 of 16
441
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.
Js
pretty-print-json.js.org
Pretty-Print JSON • Interactive online JavaScript tool to format JSON
Pretty-print JSON data into HTML to indent and colorize (written in functional TypeScript)
ZetCode
zetcode.com › javascript › json-pretty-print
JavaScript JSON Pretty Print - Formatting JSON Data
October 18, 2023 - <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script> fetch('https://jsonplaceholder.typicode.com/todos/1') .then(response => response.json()) .then(json => document.body.appendChild(document.createElement('pre')).innerHTML = JSON.stringify(json, null, 4)); </script> </body> </html> We fetch one todo object from the testing website. ... In this article we have prettified JSON output in JavaScript.
JSON Formatter
jsonformatter.org › javascript-pretty-print
Best JavaScript Pretty Print to Pretty JavaScript and Print JavaScript
Javascript Pretty Print is very unique tool for prettify json and pretty print Javascript data in color. Javascript Pretty Print support URL linking for sharing json. i.e. https://jsonformatter.org/html-pretty-print/?url=https://gist.githubusercontent.com/cbmgit/97f3fe627004a2e3df81a54129d6e917/raw/sample.html ·
CodePen
codepen.io › decodigo › pen › JjzWwr
Simple JSON Object Pretty Print.
Minimize JavaScript Editor · Fold All · Unfold All · /** * Pretty Print JSON Objects. * Inspired by http://jsfiddle.net/unLSJ/ * * @return {string} html string of the formatted JS object * @example: var obj = {"foo":"bar"}; obj.prettyPrint(); */ Object.prototype.prettyPrint = function(){ var jsonLine = /^( *)("[\w]+": )?("[^"]*"|[\w.+-]*)?([,[{])?$/mg; var replacer = function(match, pIndent, pKey, pVal, pEnd) { var key = '<span class="json-key" style="color: brown">', val = '<span class="json-value" style="color: navy">', str = '<span class="json-string" style="color: olive">', r = pIndent || ''; if (pKey) r = r + key + pKey.replace(/[": ]/g, '') + '</span>: '; if (pVal) r = r + (pVal[0] == '"' ?
JSFiddle
jsfiddle.net › unLSJ
Pretty Print JSON Data in Color - JSFiddle - Code Playground
The Code Completion will now also have the context of all panels before suggesting code to you - so if for example you have some CSS or JS, the HTML panel will suggest code based on the other two panels.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › JSON › stringify
JSON.stringify() - JavaScript - MDN Web Docs
console.log(JSON.stringify({ a: 2 }, null, " ")); /* { "a": 2 } */ Using a tab character mimics standard pretty-print appearance: js · console.log(JSON.stringify({ uno: 1, dos: 2 }, null, "\t")); /* { "uno": 1, "dos": 2 } */ Defining toJSON() for an object allows overriding its serialization behavior.
GitHub
github.com › center-key › pretty-print-json
GitHub - center-key/pretty-print-json: 🦋 Pretty-print JSON data into HTML to indent and colorize (with TypeScript declarations)
import { prettyPrintJson, FormatOptions } from 'pretty-print-json'; const data = { active: true, mode: '🚃', codes: [48348, 28923, 39080], city: 'London', }; const options: FormatOptions = { linkUrls: true }; const html: string = prettyPrintJson.toHtml(data, options); Check out the runScriptsConfig section in package.json for an interesting approach to organizing build tasks.
Starred by 140 users
Forked by 27 users
Languages TypeScript 61.4% | Shell 38.6% | TypeScript 61.4% | Shell 38.6%
DEV Community
dev.to › speaklouder › how-to-pretty-json-output-using-javascript-27k6
How to Pretty JSON Output using JavaScript - DEV Community
October 3, 2023 - In this example, we have an object jsonData, and we use JSON.stringify() with two additional arguments: null and 2. The null argument is for replacing values or functions, and the 2 specifies the number of spaces to use for indentation. This results in a nicely formatted JSON output with each key-value pair on a new line and indented by 2 spaces. The example above provides a straightforward way to pretty print JSON, but you can further customize it to suit your preferences.
Delft Stack
delftstack.com › home › howto › javascript › json stringify pretty
How to Pretty Print JSON in JavaScript | Delft Stack
March 11, 2025 - This article explains how to pretty print JSON in JavaScript, covering methods like JSON.stringify, console.table, and third-party libraries. Learn to enhance the readability of your JSON data for better debugging and comprehension. Discover practical examples and tips for effective JSON formatting ...