About
In this code snippet, we’ll learn about JSON serialization/deserialization in Javascript.
JSON or javascript object notation is a data format/structure for storing/transporting data. It’s supported by a wide variety of programming languages. It’s short, concise, easy to read/write and understand.
In Javascript, you can use the JSON.stringify() method to turn an object into a JSON string and JSON.parse() to turn a JSON string into an object.
Let’s see the example below.
Code:
//Just a demo object for serialization. let languages = { title: "Programming Languages", languages: [ "Java", "Javascript", "C", "C++", "C#", "PHP", "Python" ] } //Serialize object to json. let json = JSON.stringify(languages); console.log(json); //Deserialize json to object. let languagesDeserialized = JSON.parse(json); console.log(languagesDeserialized);