JSON
JSON stands for JavaScript Object Notation.
JSON is a syntax for storing and exchanging data.
JSON is text, written with JavaScript object notation.
JSON is a lightweight data-interchange format.
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.
Why use JSON?
Since the JSON format is text only,
it can easily be sent to and from a server,
And used as a data format by any programming
language.
JavaScript has a built in function to convert a string,
written in JSON format.
JSON.parse()
Syntax
JSON Syntax Rules
Data is in name/value pairs
Data is separated by commas
Curly braces hold objects
Square brackets hold arrays
Simple Object Call
<p id="demo"></p>
<script>
var myObj, x;
myObj = {name: "Felix", age: 20, city: "Pune"};
document.getElementById("demo").innerHTML =
myObj.name + " " + myObj.city;
</script>
Also access with this method : myObj["name"];
Output
JSON & XML
Both JSON and XML can be used to receive data from a
web server.
JSON Example
{"employees":[
{ "firstName":"John", "lastName":"Doe" },
{ "firstName":"Anna", "lastName":"Smith" },
{ "firstName":"Peter", "lastName":"Jones" }
]}
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>
Both JSON and XML are human readable.
Both JSON and XML have values within values.
Both JSON and XML can be parsed and used by lots of
programming languages.
Both JSON and XML can be fetched with an XMLHttpRequest.
JSON doesn't use end tag
JSON is shorter
JSON is quicker to read and write
JSON can use arrays
XML has to be parsed with an XML parser.
JSON can be parsed by a standard JavaScript function.
JSON is Better Than XML
JSON Strings
Strings in JSON must be written in double quotes.
{ "name":"John" }
JSON Numbers
Numbers in JSON must be an integer or a floating
point.
{ "age":30 }
JSON Objects
Values in JSON can be objects.
{
"employee":{ "name":"John", "age":30, "city":"New York" }
}
JSON Arrays
Values in JSON can be arrays.
{
"employees":[ "John", "Anna", "Peter" ]
}
JSON Booleans
Values in JSON can be true/false.
{ "sale":true }
JSON null
Values in JSON can be null.
{ "middlename":null }
JSON.parse() method
Parse the data with JSON.parse(), and the data
becomes a JavaScript object.
Example
<p id="demo"></p>
<script>
var txt = '{"name":"John", "age":30, "city":"New York"}'
var obj = JSON.parse(txt);
document.getElementById("demo").innerHTML =
obj.name + ", " + obj.age;
</script>
Output
HttpRequest Demo
<p id="demo"></p>
<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML =
myObj.name;
}
};
xmlhttp.open("GET", “filename", true);
xmlhttp.send();
</script>
<p>Take a look at <a href="demo.txt" target="_blank">demo.txt</a></p>
JSON Format Example
{
"name":"John",
"age":31,
"pets":[
{ "animal":"dog", "name":"Fido" },
{ "animal":"cat", "name":"Felix" },
{ "animal":"hamster", "name":"Lightning" }
]
}
JSON Format Example
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup
Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
JSON Format Example
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Save", "onclick": "SaveDoc()"}
]
}
}}
{
"quiz": {
"sport": {
"q1": {
"question": "Which one is correct team name in NBA?",
"options": [
"New York Bulls",
"Huston Rocket"
],
"answer": "Huston Rocket"
}
},
"maths": {
"q1": {
"question": "5 + 7 = ?",
"options": [
"10",
"13"
],
"answer": "12"
},
"q2": {
"question": "12 - 8 = ?",
"options": [
"1",
"2“
"answer": "4"
}
}
}
}
********************************************************