Convert to JSON

Mozilla dev docs

W3 Schools tutorial

Syntax

JSON.stringify(value[, replacer[, space]])

Basic

var x = {
  a: [1, 2, 3],
  b: '123'
}
JSON.stringify(x)
"{\"a\":[1,2,3],\"b\":\"123\"}"

Recommendeduse - use console.log to unescape quotes and newlines.

console.log( JSON.stringify(x) )
{"a":[1,2,3],"b":"123"}

Wrap

Wrap the output across multiline lines for readability.

Set the space argument.

The space argument may be used to control spacing in the final string.

  • Use a number to indent that many characters (up to 10).
  • Use a string to indent with that string (or the first 10 characters of it).
console.log( JSON.stringify({ a: 2 }, null, 4) )
{
    "a": 2
}

Using a tab character mimics standard pretty-print appearance:

JSON.stringify({ uno: 1, dos: 2 }, null, '\t')
{
	"uno": 1,
	"dos": 2
}