English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JSON Introduction

JSON stands for JavaScript Object Notation, a simple notation for JS objects. It is used to store and exchange data between servers and clients.

JSON is a very lightweight data interchange format that can be parsed and generated quickly and easily.

Data must be text when exchanging data between browsers and servers.

JSON is text, and we can convert any JavaScript object to JSON and then send the JSON to the server.

We can also convert any JSON received from the server to a JavaScript object.

Send data

If data is stored in a JavaScript object, you can convert the object to JSON and then send it to the server.

The following example converts a JavaScript object to a JSON string and then sends it to the server:

var myObj = {name: "Seagull", age: 22, city: "New Delhi"};
var myJSON = JSON.stringify(myObj);
window.location = "json_demo.php?q=" + myJSON;
Test see‹/›

You willJSON.stringify()Learn more about this method in the later part of this tutorial.

Receive data

If you receive data in JSON format, you can convert it to a JavaScript object.

The following example converts a string written in JSON format to a JavaScript object:

var myJSON = '{"name":"Seagull", "age":22, "city":"New Delhi"}';
var myObj = JSON.parse(myJSON);
document.getElementById("output").innerHTML = myObj.name + "lives in" + myObj.city;
Test see‹/›

You willJSON.parse()Learn more about this method in the later part of this tutorial.

Store data

JSON can store JavaScript objects as text.

The following example demonstrates how to store and retrieve user information using JSON in local storage:

// Store data
var myObj = {name: "Seagull", age:22, city: "New Delhi"};
var myJSON = JSON.stringify(myObj);
localStorage.setItem("demoJSON", myJSON);
// Retrieve Data
var data = localStorage.getItem("demoJSON");
var obj = JSON.parse(data);
document.getElementById("output").innerHTML = obj.name + "Living in" + obj.city;
Test see‹/›

For more information about the LocalStorage object, please visit ourHTML5 Web Storage API.

What is JSON?

JSON is a lightweight text-based open standard designed for human-readable data exchange.

  • JSON Representation JavaScript Object Notation

  • The JSON format was specified by Douglas Crockford

  • JSON is designed specifically for human-readable data exchange

  • JSON has been extended from the JavaScript scripting language

  • JSON is 'self-describing' and easy to understand

  • JSON is language-independent

  • The file extension for JSON is .json

  • The JSON Internet Media type is application/json

  • The unified type identifier is public.json

JSON uses JavaScript syntax, but the JSON format is just text.

It can be read by any programming language and used as a data format.

Why use JSON?

JSON format is just text and can be easily sent between servers and can be used as a data format by any programming language.

JSON format is used for serializing and transmitting structured data over network connections.

JavaScript has built-in functionsJSON.parse(), you can convert a string written in JSON format into a local JavaScript object.

Therefore, if you receive data in JSON format from the server, you can use it just like any other JavaScript object.