JAVASCRIPT

  minte9
REMEMBERS




Last update:   27-01-2022

Stringify

We'd like to convert a complex object to string, to send it over network.
 
/**
 * JSON.Stringify ...
 * converts objects to string.
 * 
 * The resulting json string is called serialized.
 * 
 * No single quotes or backticks in JSON.
 * JSON does not support comments.
 */

let student = {
    age: 30,
    courses: ['html', 'css'],
};
let json = JSON.stringify(student);
console.log(typeof json); 
    // string
console.log(json); 
    // {"age":30,"courses":["html","css"]}
  
/**
 * Nested objects ...
 * are supported and converted automatically.
 */
let meetup = {
    title: 'Conference',
    room: {
        number: 3,
        users: ['John', 'Mary']
    },
};
let meetup_json = JSON.stringify(meetup);
console.log(meetup_json);
    // {"title":"Conference","room":{"number":3,"users":["John","Mary"]}}

Parse

To decode a serialized string, we use JSON.parse.
 
/**
 * Parse JSON string
 */
let json = '{"title":"Conference","room":{"number":3}}';
let obj = JSON.parse(json);

console.log(obj);
    // { title: 'Conference', room: { number: 3 } }

Questions     github Types
Constructor
        A B C D E F
🔔
1/1