JavaScript JSON Reference Manual

JSON (JavaScript Object Notation, JavaScript Object Notation)

JSON is a format used for storing and transmitting data.

JSON is text, which can be transmitted anywhere and read by any programming language.

JavaScript objects can be converted to JSON, and JSON can be converted back to JavaScript objects.

In this way, we can use the data as a JavaScript object without complex parsing or conversion.

Example

Send JSON:

// JavaScript object...:
var myObj = { "name":"Bill", "age":19, "city":"Seattle" };
// ...Convert to JSON:
var myJSON = JSON.stringify(myObj);
// Send JSON:
window.location = "demo_json.php?x=" + myJSON;

Try It Yourself

For more knowledge about JSON, please read our JSON Tutorial.

JSON Method

Method Description
parse() Parse a JSON string and return a JavaScript object.
stringify() Convert a JavaScript object to a JSON string.

Valid data types

In JSON, values must be of the following data types:

  • String
  • Number
  • Object (including valid JSON values)
  • Array
  • Boolean
  • null

JSON values cannot be of the following data types:

  • Function
  • Date
  • undefined

More examples

Example

Receive JSON:

// myJSON is the text received in JSON format
// Convert JSON to JavaScript object:
var myObj = JSON.parse(myJSON);
document.getElementById("demo").innerHTML = myObj.name;

Try It Yourself

Example

Use localStorage to store data as JSON:

// Store data:
myObj = { "name":"Bill", "age":19, "city":"Seattle" };
myJSON = JSON.stringify(myObj);
localStorage.setItem("testJSON", myJSON);
// Retrieve data:
text = localStorage.getItem("testJSON");
obj = JSON.parse(text);
document.getElementById("demo").innerHTML = obj.name;

Try It Yourself

For more knowledge about JSON, please read our JSON Tutorial.