What is JSON?

The abbreviation JSON stands for JavaScript Object Notation.
It describes a way to format data that can be stored in files, or sent over the Internet.
The format is almost exactly the same as the syntax for defining an object literally in JavaScript, but with a few exceptions:

  • You cannot specify functions
  • Property names must be in double-quotes
  • Values cannot be expressions

The browser has a built-in object; JSON that can be used to convert between JSON, and JavaScript objects.
To make a JavaScript object into a JSON string, use the stringify method like this:

var json = {size: 5};  
var jsonString = JSON.stringify(json);  

The console output will be:


Notice how the size property name has been double-quoted.
You can make a JSON string into a JavaScript object using the parse method like this:

var jsonString = "{\"size\":5}";  
json = JSON.parse(jsonString);  

The output to the console will be:

Object {size: 5}  


JSON files usually have the file extension .json, but not always.
You can find the official description of JSON format here: http://json.org/
When you send or receive JSON over the Internet, the Content-Type header should be included with a value of application/json.
All major newer browsers have the built-in JSON object, but some older ones don't. For older browsers you can use this implementation: https://github.com/douglascrockford/JSON-js