xxxxxxxxxx
let data = {
name: "John Smith",
age: 30,
hobbies: ["Programming", "Video Games"]
};
// {name:"John Smith",age:30,hobbies:["Programming","Video Games"]}
let miny = JSON.stringify(data);
// The 4 parameter signifys 4 spaces. You can also use "\t".
/* {
* name: "John Smith",
* age: 30,
* ...
*/
let pretty = JSON.stringify(data, null, 4);
xxxxxxxxxx
json.stringify() is useful for, say, converting an object to a string format
which enbales it to be sent as data to a server or for use in other
languages
json.parse() turns a string object back into a regular object
xxxxxxxxxx
var Num=[1,2,3,4,5,6]
console.log("The Numbers Are "+JSON.stringify(Num))
//output= The Number Are [1,2,3,4,5,6]
xxxxxxxxxx
console.log(JSON.stringify({ uno: 1, dos: 2 }, null, 2));
/*
{
"uno": 1,
"dos": 2
}
*/
xxxxxxxxxx
console.log(JSON.stringify({ uno: 1, dos: 2 }, null, 6));
/*
{
"uno": 1,
"dos": 2
}
*/