how to stringify json code example

Example 1: turn object into string javascript

var obj = {a: "a", b: "b" /*...*/};
var string = JSON.stringify(obj);
// OUTPUT:
// "{'a':'a', 'b':'b'}"

Example 2: how take values from JSON.stringify($('form').serialize())

1 var formData = $('#form').serializeObject();
2 $.extend(formData, { Contacts : myContacts});
3 $.extend(formData, { Address : myAddress});
4 var result = JSON.stringify(formData);
5

Example 3: convert json.stringify to array in javascript

const myObj = {
  name: 'Skip',
  age: 2,
  favoriteFood: 'Steak'
};

const myObjStr = JSON.stringify(myObj);

console.log(myObjStr);
// "{"name":"Skip","age":2,"favoriteFood":"Steak"}"

console.log(JSON.parse(myObjStr));
// Object {name:"Skip",age:2,favoriteFood:"Steak"}

Example 4: node json stringify

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);

Example 5: json stringify and parse

//JSON STRINGIFY
var Jazzman = new Object();
Jazzman.altura = 1.85;
Jazzman.edad = 41;
Jazzman.colorOjos = "azul";
salidaJSON = JSON.stringify(Jazzman)
console.log(salidaJSON);

//JSON PARSE

var jsonTexto = '{"color":"blanco","km":100000,"esNuevo":false,"rueda":{"marca":"desconocida","estado":"malo"}}';
var coche = JSON.parse(jsonTexto);
console.log(coche);
// Resultado -> Object { color: "blanco", km: 100000, esNuevo: false, rueda: { marca: "desconocida", estado: "malo" } }

Example 6: how to use json stringify in javascript

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]