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 - const fs = require('fs'); const ... console.log('JSON saved in a pretty format.'); You can display formatted JSON in your web app using <pre> tags....
🌐
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
Author   Center Key
🌐
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, '&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.

🌐
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.
🌐
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 ...
Find elsewhere
🌐
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 ...
🌐
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.
🌐
EyeHunts
tutorial.eyehunts.com › home › javascript pretty print json in html | example code
JavaScript pretty print JSON in HTML | Example code
October 18, 2022 - To convert it to JSON and pretty print use stringify method. ... If your data is already in JSON format use the following to parse the json first. ... Simple example code to make semantic and denotes that the content inside is a code snippet.
🌐
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%
🌐
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 …
🌐
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. This is the # 1 tool to JSON Prettify. ... JSON Example with all data types including JSON Array.
🌐
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.
🌐
jQuery Script
jqueryscript.net › jquery plugins › jquery other plugins
Format JSON Data With Colored Syntax - pretty-print-json | Free jQuery Plugins
jQuery pretty-print-json is a web-based tool used to format/beautify JSON data and colorize key/value pairs depending on the data type.
🌐
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 ...