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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-pretty-print-json-string-in-javascript
How to Pretty Print JSON String in JavaScript? - GeeksforGeeks
July 12, 2025 - To pretty print JSON, you can format JSON strings with proper indentation, making them easy to read and debug. In JavaScript, you can achieve this using JSON.stringify() with optional parameters to specify the indentation level.
๐ŸŒ
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, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    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, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    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 ...
๐ŸŒ
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 - The key tool we'll use is the JSON.stringify() method, which provides a simple way to format JSON. ... const jsonData = { name: "SpeakLouder", age: 24, city: "India" }; const prettyJSON = JSON.stringify(jsonData, null, 2); console.log(prettyJSON);
๐ŸŒ
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
Find elsewhere
๐ŸŒ
JSON Formatter
jsonformatter.org โ€บ json-pretty-print
Best JSON Pretty Print Online
JSON Pretty Print helps Pretty JSON data and Print JSON data. It's very simple and easy way to prettify JSON and pretty print JSON.
๐ŸŒ
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] == '"' ?
๐ŸŒ
Medium
medium.com โ€บ @python-javascript-php-html-css โ€บ how-to-display-json-in-a-readable-format-using-javascript-34d95031a42a
How to Use JavaScript to Display JSON in a Readable Format
August 24, 2024 - Pretty-printing JSON is important because it enhances readability and helps developers debug and understand the data structure more efficiently. ... You can use the JSON.stringify method with an indentation parameter to format JSON data in ...
๐ŸŒ
Medium
medium.com โ€บ coding-at-dawn โ€บ how-to-pretty-print-json-in-just-one-line-of-javascript-html-or-react-2e79edd2b7ee
How To Pretty Print JSON in Just One Line of JavaScript + HTML (or React) | by Dr. Derek Austin ๐Ÿฅณ | Coding at Dawn | Medium
January 6, 2023 - How To Pretty Print JSON in Just One Line of JavaScript + HTML (or React) Ever need to debug a giant JSON object while working on frontend development? Iโ€™ve got you covered. When youโ€™re โ€ฆ
๐ŸŒ
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
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ javascript pretty print json in html | example code
JavaScript pretty print JSON in HTML | Example code
October 18, 2022 - use a <pre> tag to JavaScript pretty prints JSON in HTML. The <pre> need id to show data on it. Where pre tells the browser engine that the content inside is pre-formatted and can be displayed without any modification.
๐ŸŒ
SiMedia Tech
simedia.tech โ€บ blog โ€บ js-quickie-pretty-printing-a-json-object-without-external-dependencies
JS-Quickie: pretty-printing a JSON object without external dependencies
April 15, 2021 - Ever wanted to properly format a JSON string without external library? JavaScript's native JSON.stringify() provides you with exactly that functionality out of the box.
๐ŸŒ
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":
๐ŸŒ
JSON Formatter
jsonformatter.org โ€บ html-pretty-print
Best HTML Pretty Print to Pretty HTML and Print HTML
HTML Pretty Print is very unique tool for prettify json and pretty print HTML data in color.
๐ŸŒ
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.