JSON.stringify takes more optional arguments.

Try:

 JSON.stringify({a:1,b:2,c:{d:1,e:[1,2]}}, null, 4); // Indented 4 spaces
 JSON.stringify({a:1,b:2,c:{d:1,e:[1,2]}}, null, "\t"); // Indented with tab

From:

How can I beautify JSON programmatically?

It should work in modern browsers, and it is included in json2.js if you need a fallback for browsers that don't support the JSON helper functions. For display purposes, put the output in a <pre> tag to get newlines to show.

Answer from Cristian Sanchez on Stack Overflow
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ JSON โ€บ stringify
JSON.stringify() - JavaScript | MDN
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
JSON.stringify()
JSON.stringify() can not only convert objects and arrays into JSON strings, it can convert any JavaScript value into a string.
Discussions

JavaScript: How can I generate formatted easy-to-read JSON straight from an object? - Stack Overflow
Possible Duplicate: How can I beautify JSON programmatically? I know how to generate JSON from an object using JSON.stringify, or in my case the handy jQuery JSON from Google Code. Now this works ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - JSON.stringify function - Stack Overflow
In fact, It's very easy to serealize / parse javascript object with methods. Take a look at JSONfn plugin. ... doesnt stringify getter setter, also when it returns type of object said to be some kind of Object, not the Type i had defined before... More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - JSON stringify a Set - Stack Overflow
For anyone else coming here later ... this, JSON.parse takes an equivalent function called reviver that's run on everything it parses. I ended up solving this by adding "__isSet" to the start of any array made out of a set and then checking for this in the reviver function, turning it back into a set when found. 2023-04-26T00:29:40.077Z+00:00 ... While all of the above work I suggest that you subclass set and add a toJSON method to make sure that it stringify's ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
[AskJS] PETITION: Make JSON.stringify use any object's toString method internally!
.toJSON already exists, it's just a not well known feature of JS. 6th bullet point. More on reddit.com
๐ŸŒ r/javascript
16
0
January 3, 2024
๐ŸŒ
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.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
Udacity
udacity.com โ€บ blog โ€บ 2021 โ€บ 04 โ€บ javascript-json-stringify.html
Converting Javascript Objects into Strings with JSON.stringify() | Udacity
September 27, 2022 - JSON.stringify() converts Javascript objects into JSON strings. It can manage many types of data type conversion, and you can also add special conversions.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ JSON โ€บ parse
JSON.parse() - JavaScript | MDN
In order for a value to properly round-trip (that is, it gets deserialized to the same original object), the serialization process must preserve the type information. For example, you can use the replacer parameter of JSON.stringify() for this purpose:
๐ŸŒ
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.
๐ŸŒ
Reddit
reddit.com โ€บ r/javascript โ€บ [askjs] petition: make json.stringify use any object's tostring method internally!
Make JSON.stringify use any object's toString method ...
January 3, 2024 -

What do you guys think? Should I make an official proposal? What would be the best way to make such spec proposal?

And tell me what you think! The idea is for any object's toString method override the way JSON.stringify creates the string. For example:

var vec =
{
    x: 0,
    y: 0,
    toString() => `[${this.x},${this.y}]`
}

JSON.stringify(vec) //will return: '[0,0]'

This way I won't need to use a custom stream to alter a big object when converting it to JSON!

Or would you say this is very unnecessary, even if an easy thing to add to JS?

๐ŸŒ
Zhenghao
zhenghao.io โ€บ posts โ€บ json-oddities
JSON and the stringification oddities in JavaScript
Despite being of the number type, NaN and Infinity get converted into null by JSON.stringify. The rational behind the design decision is, as Crockford wrote in his book How JavaScript Works, the presence of NaN and Infinity indicates an error.
๐ŸŒ
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().
๐ŸŒ
Medium
medium.com โ€บ @akhil-mottammal โ€บ demystifying-json-stringify-and-json-parse-2699e4f0048e
Demystifying JSON.stringify() and JSON.parse() | by Akhil Mottammal | Medium
February 22, 2024 - JSON.stringify() is a method in JavaScript that converts a JavaScript object or value into a JSON string. This is useful for serializingโ€ฆ
๐ŸŒ
Keyboard Maestro
forum.keyboardmaestro.com โ€บ tips & tutorials
TIP: How to Get Readable Format from JSON.stringify() - Tips & Tutorials - Keyboard Maestro Discourse
March 20, 2017 - However, some of you, like me, may not be aware of some options of the JSON.stringify() function to generate a very readable output, and to filter the output. So, here is an example. JavaScript JSON.stringify Options //--- NORMAL, COMPACT, JSON.stringify() ...
๐ŸŒ
JavaScript.info
javascript.info โ€บ tutorial โ€บ the javascript language โ€บ data types
JSON methods, toJSON
January 24, 2024 - JavaScript provides methods JSON.stringify to serialize into JSON and JSON.parse to read from JSON.