JavaScript JSON

Back to JS page

Table of Contents

What Is JSON?

JSON stands for JavaScript Object Notation.

JSON is a lightweight data interchange format.

JSON is language independent.

JSON is "self-describing" and easy to understand.

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript JSON</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>What Is JSON?</h4>
<p id="demo"></p>

<script>
// A JSON string
const jsonString = '{"name":"John","age":30,"city":"New York"}';

// JSON syntax is similar to JavaScript object syntax
const obj = {name:"John", age:30, city:"New York"};

document.getElementById("demo").innerHTML = "JSON string: " + jsonString;
</script>

</body>
</html>

Example 1

Result View Example

Converting JSON to JavaScript

When receiving data from a web server, the data is always a string.

Parse the data with JSON.parse(), and the data becomes a JavaScript object:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript JSON</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Converting JSON to JavaScript</h4>
<p id="demo"></p>

<script>
// JSON text received from a server
const jsonText = '{"name":"John","age":30,"city":"New York"}';

// Convert JSON string to JavaScript object
const obj = JSON.parse(jsonText);

document.getElementById("demo").innerHTML =
  "Name: " + obj.name + "<br>" +
  "Age: " + obj.age + "<br>" +
  "City: " + obj.city;
</script>

</body>
</html>

Example 2

Result View Example

Converting JavaScript to JSON

When sending data to a web server, the data has to be a string.

Convert a JavaScript object into a string with JSON.stringify():

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript JSON</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Converting JavaScript to JSON</h4>
<p id="demo"></p>

<script>
// JavaScript object
const obj = {name: "John", age: 30, city: "New York"};

// Convert JavaScript object to JSON string
const jsonString = JSON.stringify(obj);

document.getElementById("demo").innerHTML = jsonString;
</script>

</body>
</html>

Example 3

Result View Example

JSON Round Trip

A common use of JSON is to exchange data to/from a web server.

When receiving data from a web server, the data is always a string. You can use JSON.parse() to convert it to a JavaScript object.

When sending data to a web server, the data has to be a string. You can use JSON.stringify() to convert it to JSON.

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript JSON</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>JSON Round Trip</h4>
<p id="demo"></p>

<script>
// Start with a JavaScript object
const obj1 = {name: "John", age: 30, city: "New York"};

// Convert to JSON string (sending to server)
const jsonString = JSON.stringify(obj1);

// Convert back to JavaScript object (receiving from server)
const obj2 = JSON.parse(jsonString);

document.getElementById("demo").innerHTML =
  "Original: " + obj1.name + "<br>" +
  "JSON: " + jsonString + "<br>" +
  "Back to object: " + obj2.name;
</script>

</body>
</html>

Example 4

Result View Example

Storing Data

JSON can be used to store data in localStorage:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript JSON</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Storing Data</h4>
<p id="demo"></p>

<script>
// Create an object
const person = {
  name: "John",
  age: 30,
  city: "New York"
};

// Store as JSON in localStorage
localStorage.setItem("person", JSON.stringify(person));

// Retrieve and parse from localStorage
const storedPerson = JSON.parse(localStorage.getItem("person"));

document.getElementById("demo").innerHTML =
  "Stored name: " + storedPerson.name + "<br>" +
  "Stored age: " + storedPerson.age + "<br>" +
  "Stored city: " + storedPerson.city;
</script>

</body>
</html>

Example 5

Result View Example

Document

Document in project

You can Download PDF file.

Reference