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
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
6935
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.
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 ...
Tutorial Republic
tutorialrepublic.com › faq › how-to-pretty-print-json-using-javascript.php
How to Pretty-print JSON Using JavaScript
// Sample JSON string var json = '{"name": "Peter", "age": 22, "country": "United States"}'; // Converting JSON string to object var obj = JSON.parse(json); // Pretty printing var str = JSON.stringify(obj, null, 4); // spacing level = 4 console.log(str); ... Is this website helpful to you? Please give us a like, or share your feedback to help us improve. Connect with us on Facebook and Twitter for the latest updates. ... Bootstrap Icon Search Utility HTML Formatter Title & Meta Length Calculator HTML Color Picker Bootstrap Button Generator SQL Playground Font Awesome Icon Finder HTML Editor
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] == '"' ?
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
Published Feb 27, 2026
Version 3.0.7
JSON Formatter
jsonformatter.org › javascript-pretty-print
Best JavaScript Pretty Print to Pretty JavaScript and Print JavaScript
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 · No. It's not required to save and share code. If Javascript data is saved without login, it will become public.
ZetCode
zetcode.com › javascript › json-pretty-print
JavaScript JSON Pretty Print - Formatting JSON Data
October 18, 2023 - Web browsers do not automatically pretty print JSON output; we can prettify the JSON output with JSON.stringify. The JSON.stringify function has a space option, which inserts white space into the output JSON string for readability purposes.
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.
TutorialsPoint
tutorialspoint.com › how-to-pretty-print-json-using-javascript
How to pretty print json using javascript?
August 23, 2022 - <!DOCTYPE html> <html> <head> <title>HTML Console</title> </head> <body> <h3> Output Console </h3> <p> Output: </p> <pre id="output"> </pre> <div id="opError" style="color : #ff0000"> </div> <script> var content = '' var error = '' var opDiv = document.querySelector('#output') var opErrDiv = document.querySelector('#opError') // actual javascript code try { var vehicle = { id: 'v01', type: 'bus', length: 6 } var book = { "b_id": "b_001", "author": { "lastname": "Paul", "firstname": "Alice" }, "editor": { "lastname": "Smith", "firstname": "Bob" }, "title": "A sample book for JSON", "category":
JSONBin
jsonbin.io › blog › 1 › how-to-pretty-print-a-json-object-with-javascript
How to pretty-print a JSON object with JavaScript - JSONBin.io
It simply does not convert a JavaScript object or a value to a JSON string but it also provides options to replace values using a replacer function (which is out of scope for this article) as well as space option which lets you define the tab space you wish to pass which will indent and pretty-print the stringified JSON.