🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › JSON › stringify
JSON.stringify() - JavaScript | MDN
2 weeks ago - The JSON.stringify() static method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.
🌐
W3Schools
w3schools.com › js › js_json_stringify.asp
JavaScript JSON.stringify()
The JSON.stringify() method converts a JavaScript value into JSON text.
🌐
JSONLint
jsonlint.com › json-stringify
JSON Stringify - Escape JSON for Embedding | JSONLint
JSON Stringify converts a JSON object into an escaped string. The result can be safely embedded inside another string—in code, databases, or even nested within other JSON.
🌐
JSON Formatter
jsonformatter.org › json-stringify-online
JSON Stringify Online using JSON.Stringify()
JSON Stringify Online helps convert string value to JSON String using JSON.Stringify().
🌐
W3Schools
w3schools.com › JS › › js_json_stringify.asp
W3Schools.com
The JSON.stringify() method converts a JavaScript value into JSON text.
🌐
Zhenghao
zhenghao.io › posts › json-oddities
JSON and the stringification oddities in JavaScript
In JavaScript, the way to convert a value to a JSON string is via JSON.stringify.
🌐
Medium
medium.com › @debbie.obrien › json-parse-v-json-stringify-4b9d104c78d0
JSON Parse v JSON Stringify. I always get confused between the JSON… | by Debbie O'Brien | Medium
May 28, 2018 - JSON.stringify() converts a JavaScript object to a JSON string. When sending data to a web server, the data has to be a string.
🌐
V8
v8.dev › blog › json-stringify
How we made JSON.stringify more than twice as fast · V8
At the end, the final result is constructed by simply concatenating the output from the initial one-byte stringifier with the output from the two-byte one. This strategy ensures we stay on a highly-optimized path for the common case, while the transition to handling two-byte characters is lightweight and efficient. Any string in JavaScript can contain characters that require escaping when serializing to JSON (e.g.
Find elsewhere
🌐
Execute Program
executeprogram.com › courses › modern-javascript › lessons › json-stringify-and-parse
Modern JavaScript: JSON Stringify and Parse
Learn programming languages like TypeScript, Python, JavaScript, SQL, and regular expressions. Interactive with real code examples.
🌐
Online Tools
onlinetools.com › json › stringify-json
Stringify JSON – Online JSON Tools
We call the JSON.stringify() function on the input data and it converts line breaks into "\n" escape sequences, quotes into "\"" escape sequences, and tabs into "\t" escape sequences.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-json-stringify-method
JavaScript JSON stringify() Method - GeeksforGeeks
July 11, 2025 - The JSON.stringify() method in JavaScript is used to convert JavaScript objects into a JSON string.
🌐
YouTube
youtube.com › watch
Let's Unpack JSON Stringify - YouTube
Let's have a look at the hidden gems found in JSON.Stringify - there's more than you think. And it's not just indentation.For your reference, check this out:...
Published: September 17, 2024
🌐
ServiceNow Community
servicenow.com › community › developer-articles › json-stringify-making-json-look-pretty-and-perfect › ta-p › 2534944
JSON.stringify() - Making JSON Look Pretty and Per... - ServiceNow Community
December 9, 2025 - So, how do we use JSON.stringify()? It's actually quite simple. All you need is your JSON object and the stringify() method. And if you want to make it look even prettier, just add null and '\t' as the second and third parameters.
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:

Show code snippet

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; }
Run code snippetEdit code snippet Hide Results Copy to answer Expand

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.

🌐
DEV Community
dev.to › katherineh › jsonstringify-jsonparse-1585
JSON.stringify() & JSON.parse() - DEV Community
November 18, 2024 - JSON.stringify() converts JavaScript Objects an Arrays into JSON Strings.
🌐
MindStick
mindstick.com › forum › 161281 › what-does-json-stringify-do-in-javascript
What Does JSON.stringify() Do in JavaScript? – MindStick
March 20, 2025 - The JSON.stringify() method JavaScript converts JavaScript objects and arrays into JSON-formatted strings.
🌐
freeCodeCamp
freecodecamp.org › news › json-stringify-example-how-to-parse-a-json-object-with-javascript
JSON Stringify Example – How to Parse a JSON Object with JS
January 5, 2021 - And just like that, you've parsed incoming JSON with fetch and used JSON.stringify() to convert a JS object literal into a JSON string.
🌐
Testmuai
testmuai.com › home › free tools › json stringify online
JSON Stringify Online | Free online tool to Stringify JSON | TestMu AI (Formerly LambdaTest)
To exchange data in JSON format, ... the network or stored in a database. The JSON stringify function converts an object or array into a JSON-formatted string....
🌐
ServiceNow Community
servicenow.com › community › developer-articles › interesting-facts-about-json-stringify › ta-p › 2329985
Interesting facts about JSON.stringify() - ServiceNow Community
June 5, 2021 - We all know the primary purpose for JSON.stringify(), The JSON.stringify() method converts a JavaScript object or value to a JSON string. However there are 2 interesting parameter that can be used to simplify developer's job. One can be used to filter the JSON and other can be used to indent it.