JavaScript: JSON

JSON(JavaScript Object Notation) is very useful when you work with data. The first thing we should know is how to convert an object to JSON and a JSON to an object. There is a simple way to do it.

stringify (Object -> JSON)

const person = {
    name: 'grace',
    age: 20,
    isMarried: false,
};

let json = JSON.stringify(person);
console.log(json);

result

image.png

Also, you can choose a property you will serialize.

json = JSON.stringify(person, ['name', 'age']);
console.log(json);

result

image.png

parse (JSON -> Object)

const object = JSON.parse(json); 
console.log(object);
  • In here, 'json' is from the above code.

result

image.png