Javascript JSON

JS Code Snippets JSON
Share:

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

Resulting output:

Share:

Leave a Reply

Your email address will not be published. Required fields are marked *

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger