Chapter 3 Json
Chapter 3 Json
What is JSON
o JSON stands for JavaScript Object Notation.
o JSON is a text format for storing and transporting data
o JSON is an open standard data-interchange format.
o JSON is lightweight and self-describing.
o JSON originated from JavaScript.
o JSON is easy to read and write.
o JSON is language independent.
o JSON supports data structures such as arrays and objects.
The JSON syntax is derived from JavaScript object notation, but the JSON format
is text only.
Code for reading and generating JSON exists in many programming languages.
JSON Example
File: first.json
{"employees":[
{"name":"Sonoo", "email":"[email protected]"},
{"name":"Rahul", "email":"[email protected]"},
{"name":"John", "email":"[email protected]"} ]
}
Features of JSON
o Simplicity
o Openness
o Self-Describing
o Internationalization
o Extensibility
o Interoperability
The JSON format is syntactically similar to the code for creating JavaScript
objects. Because of this, a JavaScript program can easily convert JSON data into
JavaScript objects.
Since the format is text only, JSON data can easily be sent between computers, and
used by any programming language.
JavaScript has a built in function for converting JSON strings into JavaScript
objects:
JSON.parse()
JavaScript also has a built in function for converting an object into a JSON string:
JSON.stringify()
Used to receive pure text from a server and use it as a JavaScript object.
Used to work with data as JavaScript objects, with no complicated parsing and
translations.
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
{name:"John"}
JSON Values
a string
a number
an 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
1. JSON Strings
E.g : {"name":"John"}
2. JSON Numbers
1. JSON Objects
{
"employee":{"name":"John", "age":30, "city":"New York"}
}
2. JSON Arrays
{
"employees":["John", "Anna", "Peter"]
}
3. JSON Booleans
{"sale":true}
6 JSON null
{"middlename":null}
JSON
{"name":"John"}
In JavaScript, you can write string values with double or single quotes:
JavaScript
{name:'John'}
JavaScript Objects
Because JSON syntax is derived from JavaScript object notation, very little extra
software is needed to work with JSON within JavaScript.
With JavaScript you can create an object and assign data to it, like this:
Example
person = {name:"John", age:31, city:"New York"};
Example
// returns John
person.name;
Example
// returns John
person["name"];
Example
person.name = "Gilbert";
Example
person["name"] = "Gilbert"
<html>
<body>
<h2>Access a JavaScript object</h2>
<p id="demo"></p>
<script>
const myObj = {name:"John", age:30, city:"New York"};
document.getElementById("demo").innerHTML =
myObj.city;
</script>
</body>
</html>
JSON vs XML
Both JSON and XML can be used to receive data from a web server.
The following JSON and XML examples both define an employee’s object, with
an array of 3 employees:
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>
XML has to be parsed with an XML parser. JSON can be parsed by a standard
JavaScript function.
Using XML
Using JSON
The following are the differences between the json and xml:
JSON XML
JSON stands for javascript object XML stands for an extensible markup language.
notation.
The extension of json file is .json. The extension of xml file is .xml.
The internet media type is The internet media type is application/xml or text/xm
application/json.
The type of format in JSON is data The type of format in XML is a markup language.
interchange.
The object created in JSON has some XML data does not have any type.
type.
The data types supported by JSON are XML data is in a string format.
strings, numbers, Booleans, null, array.
It does not have any capacity to display XML is a markup language, so it has the capac
the data. display the content.
JSON has no tags. XML data is represented in tags, i.e., start tag and en
JSON is quicker to read and write. XML file takes time to read and write becau
learning curve is higher.
JSON can use arrays to represent the XML does not contain the concept of arrays.
data.
It can be parsed by a standard javascript XML data which is used to interchange the data, m
function. It has to be parsed before use. parsed with respective to their programming langu
use that.
JSON.parse()
A common use of JSON is to exchange data to/from a web server. 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.
Use the JavaScript function JSON.parse() to convert text into a JavaScript object:
Make sure the text is in JSON format, or else you will get a syntax error.
Example
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = obj.name;
</script>
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
const text = '["Ford", "BMW", "Audi", "Fiat"]';
const myArr = JSON.parse(text);
JSON.stringify()
A common use of JSON is to exchange data to/from a web server. When sending
data to a web server, the data has to be a string. Convert a JavaScript object into a
string with JSON.stringify().
Example
const obj = {name: "abc", age: 30, city: "India"};
const myJSON = JSON.stringify(obj);
You will learn how to send JSON to a server in the next chapters.
Stringify a JavaScript Array
Example
const arr = ["John", "Peter", "Sally", "Jane"];
const myJSON = JSON.stringify(arr);
JSON Comments
But you can do some tricks such as adding extra attribute for comment in JSON
object, for example:
{
"employee": {
"name": "Bob",
"salary": 56000,
"comments": "Hello good morning"
}
}