JSON.stringify()
Converts JS object or value to a JSON string
JSON.stringify({ x: 5, y: 6 }); // "{"x":5,"y":6}" JSON.stringify({}) // '{}' JSON.stringify(true) // 'true' JSON.stringify('foo') // '"foo"' JSON.stringify([1, 'false', false]) // '[1,"false",false]' JSON.stringify([NaN, null, Infinity]) // '[null,null,null]' JSON.stringify({ x: 5 }) // '{"x":5}' JSON.stringify(new Date(2006, 0, 2, 15, 4, 5)) // '"2006-01-02T15:04:05.000Z"'
Can stringify array of objects
JSON.stringify( [ new Number(3), new String('false'), new Boolean(false) ] ); // "[3,"false",false]"
Some data types will be forced to null
when stringified
JSON.stringify( { x: [10, undefined, function(){}, Symbol('')] } ); // "{"x":[10,null,null,null]}"
Function has two parts:
When your function is serialized, it loses the context/environment after it is de-serialized and called.
var someValue = "something"; var obj = { propertyName: "A value", functionName: function(){ // do something with someValue variable // after being deserialized, funciton will not know about `someValue` } }
JSON.parse()
Method to construct the JavaScript value or object described by the string.
const json = '{"result":true, "count":42}'; const obj = JSON.parse(json); console.log(obj.count); // Expected output: 42 console.log(obj.result); // Expected output: true