Professional Documents
Culture Documents
W3schools: JSON - Introduction
W3schools: JSON - Introduction
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
JSON - Introduction
❮ Previous Next ❯
Exchanging Data
When exchanging data between a browser and a server, the data can only be text.
JSON is text, and we can convert any JavaScript object into JSON, and send JSON to the server.
We can also convert any JSON received from the server into JavaScript objects.
https://www.w3schools.com/js/js_json_intro.asp 1/6
2/14/2018 JSON Introduction
This way we can work with the data as JavaScript objects, with no complicated parsing and translations.
Sending Data
If you have data stored in a JavaScript object, you can convert the object into JSON, and send it to a server:
Example
Try it Yourself »
You will learn more about the JSON.stringify() function later in this tutorial.
Receiving Data
If you receive data in JSON format, you can convert it into a JavaScript object:
Example
https://www.w3schools.com/js/js_json_intro.asp 2/6
2/14/2018 JSON Introduction
Try it Yourself »
You will learn more about the JSON.parse() function later in this tutorial.
Storing Data
When storing data, the data has to be a certain format, and regardless of where you choose to store it, text is always one of
the legal formats.
Example
Storing data in local storage
//Storing data:
myObj = { "name":"John", "age":31, "city":"New York" };
myJSON = JSON.stringify(myObj);
localStorage.setItem("testJSON", myJSON);
//Retrieving data:
text = localStorage.getItem("testJSON");
obj = JSON.parse(text);
document.getElementById("demo").innerHTML = obj.name;
https://www.w3schools.com/js/js_json_intro.asp 3/6
2/14/2018 JSON Introduction
Try it Yourself »
What is JSON?
JSON stands for JavaScript Object Notation
JSON is a lightweight data-interchange format
JSON is "self-describing" and easy to understand
JSON is language independent *
*
JSON uses JavaScript syntax, but the JSON format is text only.
Text can be read and used as a data format by any programming language.
JavaScript has a built in function to convert a string, written in JSON format, into native JavaScript objects:
JSON.parse()
So, if you receive data from a server, in JSON format, you can use it like any other JavaScript object.
❮ Previous Next ❯
https://www.w3schools.com/js/js_json_intro.asp 4/6
2/14/2018 JSON Introduction
COLOR PICKER
HOW TO
Tabs
Dropdowns
Accordions
Convert Weights
Animated Buttons
Side Navigation
Top Navigation
Modal Boxes
Progress Bars
Parallax
Login Form
HTML Includes
Google Maps
Range Sliders
Tooltips
Slideshow
Filter List
Sort List
https://www.w3schools.com/js/js_json_intro.asp 5/6
2/14/2018 JSON Introduction
SHARE
CERTIFICATES
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML.
Read More »
https://www.w3schools.com/js/js_json_intro.asp 6/6
2/14/2018 JSON Syntax
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
JSON Syntax
❮ Previous Next ❯
https://www.w3schools.com/js/js_json_syntax.asp 1/7
2/14/2018 JSON Syntax
A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value:
Example
"name":"John"
JSON
{ "name":"John" }
JavaScript
https://www.w3schools.com/js/js_json_syntax.asp 2/7
2/14/2018 JSON Syntax
{ name:"John" }
JSON Values
In JSON, values must be one of the following data types:
a string
a number
an object (JSON object)
an array
a boolean
null
In JavaScript values can be all of the above, plus any other valid JavaScript expression, including:
a function
a date
undefined
JSON
{ "name":"John" }
In JavaScript, you can write string values with double or single quotes:
https://www.w3schools.com/js/js_json_syntax.asp 3/7
2/14/2018 JSON Syntax
JavaScript
{ name:'John' }
With JavaScript you can create an object and assign data to it, like this:
Example
Example
// returns John
person.name;
Try it Yourself »
Example
// returns John
person["name"];
Try it Yourself »
Example
person.name = "Gilbert";
Try it Yourself »
Example
person["name"] = "Gilbert";
Try it Yourself »
You will learn how to convert JavaScript objects into JSON later in this tutorial.
https://www.w3schools.com/js/js_json_syntax.asp 5/7
2/14/2018 JSON Syntax
You will learn more about arrays as JSON later in this tutorial.
JSON Files
The file type for JSON files is ".json"
The MIME type for JSON text is "application/json"
❮ Previous Next ❯
COLOR PICKER
HOW TO
Tabs
Dropdowns
Accordions
https://www.w3schools.com/js/js_json_syntax.asp 6/7
2/14/2018 JSON Syntax
Convert Weights
Animated Buttons
Side Navigation
Top Navigation
Modal Boxes
Progress Bars
Parallax
Login Form
HTML Includes
Google Maps
Range Sliders
Tooltips
Slideshow
Filter List
Sort List
SHARE
CERTIFICATES
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML.
Read More »
https://www.w3schools.com/js/js_json_syntax.asp 7/7
2/14/2018 JSON vs XML
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
JSON vs XML
❮ Previous Next ❯
Both JSON and XML can be used to receive data from a web server.
The following JSON and XML examples both defines an employees object, with an array of 3 employees:
JSON Example
{"employees":[
{ "firstName":"John", "lastName":"Doe" },
{ "firstName":"Anna", "lastName":"Smith" },
{ "firstName":"Peter", "lastName":"Jones" }
]}
https://www.w3schools.com/js/js_json_xml.asp 1/4
2/14/2018 JSON vs XML
XML Example
<employees>
<employee>
<firstName>John</firstName> <lastName>Doe</lastName>
</employee>
<employee>
<firstName>Anna</firstName> <lastName>Smith</lastName>
</employee>
<employee>
<firstName>Peter</firstName> <lastName>Jones</lastName>
</employee>
</employees>
XML has to be parsed with an XML parser. JSON can be parsed by a standard JavaScript function.
Using XML
Using JSON
❮ Previous Next ❯
COLOR PICKER
https://www.w3schools.com/js/js_json_xml.asp 3/4
2/14/2018 JSON vs XML
HOW TO
Tabs
Dropdowns
Accordions
Convert Weights
Animated Buttons
Side Navigation
Top Navigation
Modal Boxes
Progress Bars
Parallax
Login Form
HTML Includes
Google Maps
Range Sliders
Tooltips
Slideshow
Filter List
Sort List
SHARE
https://www.w3schools.com/js/js_json_xml.asp 4/4
2/14/2018 JSON Data Types
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
a string
a number
an object (JSON object)
an array
a boolean
null
a function
https://www.w3schools.com/js/js_json_datatypes.asp 1/5
2/14/2018 JSON Data Types
a date
undefined
JSON Strings
Strings in JSON must be written in double quotes.
Example
{ "name":"John" }
JSON Numbers
Numbers in JSON must be an integer or a floating point.
Example
{ "age":30 }
JSON Objects
Values in JSON can be objects.
https://www.w3schools.com/js/js_json_datatypes.asp 2/5
2/14/2018 JSON Data Types
Example
{
"employee":{ "name":"John", "age":30, "city":"New York" }
}
Objects as values in JSON must follow the same rules as JSON objects.
JSON Arrays
Values in JSON can be arrays.
Example
{
"employees":[ "John", "Anna", "Peter" ]
}
JSON Booleans
https://www.w3schools.com/js/js_json_datatypes.asp 3/5
2/14/2018 JSON Data Types
Example
{ "sale":true }
JSON null
Values in JSON can be null.
Example
{ "middlename":null }
❮ Previous Next ❯
COLOR PICKER
https://www.w3schools.com/js/js_json_datatypes.asp 4/5
2/14/2018 JSON Data Types
HOW TO
Tabs
Dropdowns
Accordions
Convert Weights
Animated Buttons
Side Navigation
Top Navigation
Modal Boxes
Progress Bars
Parallax
Login Form
HTML Includes
Google Maps
Range Sliders
Tooltips
Slideshow
Filter List
Sort List
SHARE
CERTIFICATES
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML.
https://www.w3schools.com/js/js_json_datatypes.asp 5/5
2/14/2018 JSON Objects
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
JSON Objects
❮ Previous Next ❯
Object Syntax
Example
Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null).
https://www.w3schools.com/js/js_json_objects.asp 1/7
2/14/2018 JSON Objects
Example
Try it Yourself »
You can also access the object values by using bracket ([]) notation:
Example
Try it Yourself »
https://www.w3schools.com/js/js_json_objects.asp 2/7
2/14/2018 JSON Objects
Looping an Object
You can loop through object properties by using the for-in loop:
Example
Try it Yourself »
In a for-in loop, use the bracket notation to access the property values:
Example
Try it Yourself »
https://www.w3schools.com/js/js_json_objects.asp 3/7
2/14/2018 JSON Objects
Example
myObj = {
"name":"John",
"age":30,
"cars": {
"car1":"Ford",
"car2":"BMW",
"car3":"Fiat"
}
}
You can access nested JSON objects by using the dot notation or bracket notation:
Example
x = myObj.cars.car2;
//or:
x = myObj.cars["car2"];
Try it Yourself »
https://www.w3schools.com/js/js_json_objects.asp 4/7
2/14/2018 JSON Objects
Modify Values
You can use the dot notation to modify any value in a JSON object:
Example
myObj.cars.car2 = "Mercedes";
Try it Yourself »
You can also use the bracket notation to modify a value in a JSON object:
Example
myObj.cars["car2"] = "Mercedes";
Try it Yourself »
Example
https://www.w3schools.com/js/js_json_objects.asp 5/7
2/14/2018 JSON Objects
delete myObj.cars.car2;
Try it Yourself »
❮ Previous Next ❯
COLOR PICKER
HOW TO
Tabs
Dropdowns
Accordions
Convert Weights
Animated Buttons
Side Navigation
Top Navigation
Modal Boxes
Progress Bars
Parallax
https://www.w3schools.com/js/js_json_objects.asp 6/7
2/14/2018 JSON Objects
Login Form
HTML Includes
Google Maps
Range Sliders
Tooltips
Slideshow
Filter List
Sort List
SHARE
CERTIFICATES
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML.
Read More »
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
JSON Arrays
❮ Previous Next ❯
Example
In JSON, array values must be of type string, number, object, array, boolean or null.
In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates,
and undefined.
https://www.w3schools.com/js/js_json_arrays.asp 1/6
2/14/2018 JSON Arrays
Example
{
"name":"John",
"age":30,
"cars":[ "Ford", "BMW", "Fiat" ]
}
Example
x = myObj.cars[0];
Try it Yourself »
Example
for (i in myObj.cars) {
x += myObj.cars[i];
}
Try it Yourself »
Example
Try it Yourself »
Example
myObj = {
"name":"John",
"age":30,
"cars": [
{ "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
{ "name":"BMW", "models":[ "320", "X3", "X5" ] },
{ "name":"Fiat", "models":[ "500", "Panda" ] }
]
}
To access arrays inside arrays, use a for-in loop for each array:
Example
for (i in myObj.cars) {
x += "<h1>" + myObj.cars[i].name + "</h1>";
for (j in myObj.cars[i].models) {
x += myObj.cars[i].models[j];
}
}
Try it Yourself »
https://www.w3schools.com/js/js_json_arrays.asp 4/6
2/14/2018 JSON Arrays
Example
myObj.cars[1] = "Mercedes";
Try it Yourself »
Example
delete myObj.cars[1];
Try it Yourself »
❮ Previous Next ❯
https://www.w3schools.com/js/js_json_arrays.asp 5/6
2/14/2018 JSON Arrays
COLOR PICKER
HOW TO
Tabs
Dropdowns
Accordions
Convert Weights
Animated Buttons
Side Navigation
Top Navigation
Modal Boxes
Progress Bars
Parallax
Login Form
HTML Includes
Google Maps
Range Sliders
Tooltips
Slideshow
Filter List
Sort List
SHARE
https://www.w3schools.com/js/js_json_arrays.asp 6/6
2/14/2018 JSON.parse()
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
JSON.parse()
❮ Previous Next ❯
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.
https://www.w3schools.com/js/js_json_parse.asp 1/8
2/14/2018 JSON.parse()
Use the JavaScript function JSON.parse() to convert text into a JavaScript object:
Make sure the text is written in JSON format, or else you will get a syntax error.
Example
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = obj.name + ", " + obj.age;
</script>
Try it Yourself »
As long as the response from the server is written in JSON format, you can parse the string into a JavaScript object.
Example
https://www.w3schools.com/js/js_json_parse.asp 2/8
2/14/2018 JSON.parse()
Try it Yourself »
Array as JSON
When using the JSON.parse() on a JSON derived from an array, the method will return a JavaScript array, instead of a
JavaScript object.
Example
The JSON returned from the server is an array:
https://www.w3schools.com/js/js_json_parse.asp 3/8
2/14/2018 JSON.parse()
Try it Yourself »
Exceptions
Parsing Dates
Date objects are not allowed in JSON.
Example
Convert a string into a date:
https://www.w3schools.com/js/js_json_parse.asp 4/8
2/14/2018 JSON.parse()
Try it Yourself »
Or, you can use the second parameter, of the JSON.parse() function, called reviver.
The reviver parameter is a function that checks each property, before returning the value.
Example
Convert a string into a date, using the reviver function:
Try it Yourself »
https://www.w3schools.com/js/js_json_parse.asp 5/8
2/14/2018 JSON.parse()
Parsing Functions
Functions are not allowed in JSON.
Example
Convert a string into a function:
Try it Yourself »
You should avoid using functions in JSON, the functions will lose their scope, and you would have to use eval() to
convert them back into functions.
Browser Support
The JSON.parse() function is included in all major browsers and in the latest ECMAScript (JavaScript) standard:
https://www.w3schools.com/js/js_json_parse.asp 6/8
2/14/2018 JSON.parse()
Firefox 3.5
Internet Explorer 8
Chrome
Opera 10
Safari 4
❮ Previous Next ❯
COLOR PICKER
HOW TO
Tabs
Dropdowns
Accordions
Convert Weights
https://www.w3schools.com/js/js_json_parse.asp 7/8
2/14/2018 JSON.parse()
Animated Buttons
Side Navigation
Top Navigation
Modal Boxes
Progress Bars
Parallax
Login Form
HTML Includes
Google Maps
Range Sliders
Tooltips
Slideshow
Filter List
Sort List
SHARE
CERTIFICATES
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML.
Read More »
https://www.w3schools.com/js/js_json_parse.asp 8/8
2/14/2018 JSON.stringify()
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
JSON.stringify()
❮ Previous Next ❯
https://www.w3schools.com/js/js_json_stringify.asp 1/7
2/14/2018 JSON.stringify()
Example
Try it Yourself »
You will learn how to send JSON to the server in the next chapter.
Example
Try it Yourself »
You will learn how to send JSON to the server in the next chapter.
Exceptions
https://www.w3schools.com/js/js_json_stringify.asp 3/7
2/14/2018 JSON.stringify()
Stringify Dates
In JSON, date objects are not allowed. The JSON.stringify() function will convert any dates into strings.
Example
document.getElementById("demo").innerHTML = myJSON;
Try it Yourself »
You can convert the string back into a date object at the receiver.
Stringify Functions
In JSON, functions are not allowed as object values.
The JSON.stringify() function will remove any functions from a JavaScript object, both the key and the value:
Example
document.getElementById("demo").innerHTML = myJSON;
https://www.w3schools.com/js/js_json_stringify.asp 4/7
2/14/2018 JSON.stringify()
Try it Yourself »
This can be omitted if you convert your functions into strings before running the JSON.stringify() function.
Example
document.getElementById("demo").innerHTML = myJSON;
Try it Yourself »
You should avoid using functions in JSON, the functions will lose their scope, and you would have to use eval() to
convert them back into functions.
Browser Support
The JSON.stringify() function is included in all major browsers and in the latest ECMAScript (JavaScript) standard:
Firefox 3.5
Internet Explorer 8
https://www.w3schools.com/js/js_json_stringify.asp 5/7
2/14/2018 JSON.stringify()
Chrome
Opera 10
Safari 4
❮ Previous Next ❯
COLOR PICKER
HOW TO
Tabs
Dropdowns
Accordions
Convert Weights
Animated Buttons
Side Navigation
Top Navigation
Modal Boxes
Progress Bars
Parallax
https://www.w3schools.com/js/js_json_stringify.asp 6/7
2/14/2018 JSON.stringify()
Login Form
HTML Includes
Google Maps
Range Sliders
Tooltips
Slideshow
Filter List
Sort List
SHARE
CERTIFICATES
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML.
Read More »
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
JSON HTML
❮ Previous Next ❯
HTML Table
Make an HTML table with data received as JSON:
Example
Try it Yourself »
Example
https://www.w3schools.com/js/js_json_html.asp 2/6
2/14/2018 JSON HTML
<option value="products">Products</option>
<option value="suppliers">Suppliers</option>
</select>
<script>
function change_myselect(sel) {
var obj, dbParam, xmlhttp, myObj, x, txt = "";
obj = { "table":sel, "limit":20 };
dbParam = JSON.stringify(obj);
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myObj = JSON.parse(this.responseText);
txt += "<table border='1'>"
for (x in myObj) {
txt += "<tr><td>" + myObj[x].name + "</td></tr>";
}
txt += "</table>"
document.getElementById("demo").innerHTML = txt;
}
};
xmlhttp.open("POST", "json_demo_db_post.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("x=" + dbParam);
}
</script>
Try it Yourself »
https://www.w3schools.com/js/js_json_html.asp 3/6
2/14/2018 JSON HTML
Example
Try it Yourself »
https://www.w3schools.com/js/js_json_html.asp 4/6
2/14/2018 JSON HTML
❮ Previous Next ❯
COLOR PICKER
HOW TO
Tabs
Dropdowns
Accordions
Convert Weights
Animated Buttons
Side Navigation
Top Navigation
Modal Boxes
Progress Bars
Parallax
Login Form
HTML Includes
Google Maps
Range Sliders
Tooltips
Slideshow
https://www.w3schools.com/js/js_json_html.asp 5/6
2/14/2018 JSON HTML
Filter List
Sort List
SHARE
CERTIFICATES
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML.
Read More »
https://www.w3schools.com/js/js_json_html.asp 6/6