Unit 3
Unit 3
JSON, short for JavaScript Object Notation, is a format for sharing data. As its name
suggests, JSON is derived from the JavaScript programming language, but it’s available
for use by many languages including Python, Ruby, PHP, and Java. JSON is usually
pronounced like the name “Jason.”
JSON is also readable, lightweight, offers a good alternative to XML, and requires much
less formatting.
JSON is a data format that is not dependent on any language. It is a data format used by
several modern programming languages. JSON is used in electronic data exchange, such
as transmitting data in web applications. Websites are made of web pages. These web
pages display pre-stored information in a server and interact with the server using data
formats such as JSON.
JSON format has a syntax nearly identical to the code for JavaScript objects. This
similarity makes it very easy for programs written in JavaScript to be converted to a
JSON data format. Even though JSON is derived from JavaScript object notation syntax,
JSON is a text-only subset of JavaScript syntax.
It gained so much popularity because of its ease of use and simplicity. The JSON data
format replaced XML, which was formally in use but was very heavy and difficult to
learn due to several modifications.
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
application/json. text/xml.
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 XML data does not have any type.
some type.
It does not have any capacity to XML is a markup language, so it has the capacity
display the data. to display the content.
JSON has no tags. XML data is represented in tags, i.e., start tag
and end tag.
JSON is quicker to read and write. XML file takes time to read and write because
the learning curve is higher.
JSON can use arrays to represent XML does not contain the concept of arrays.
the data.
It can be parsed by a standard XML data which is used to interchange the data,
javascript function. It has to be must be parsed with respective to their
parsed before use. programming language to use that.
It can be easily parsed and little bit It is difficult to parse.
code is required to parse the data.
JSON uses the .json extension when it stands alone, and when it’s defined in another
file format (as in .html), it can appear inside of quotes as a JSON string, or it can be an
object assigned to a variable. This format transmits between web server and client or
browser.
“cricketer”: [
“matches”: “1586”,
“runs”: “15000”,
“centuries”: “100”
]
JSON keys are on the left side of the colon. They need to be wrapped in double
quotation marks, as in "key", and can be any valid string. Within each object, keys need
to be unique. These key strings can include whitespaces
JSON values are found to the right of the colon. At the granular level, these need to be
one of following six data types:
strings
numbers
objects
arrays
Booleans (true or false)
null
Each of the data types that are passed in as values into JSON will maintain their own
syntax, meaning strings will be in quotes, but numbers will not be.
a JSON object is not the same format as a JavaScript object, so though you can use
functions within JavaScript objects, you cannot use them as values in JSON. The most
important attribute of JSON is that it can be readily transferred between programming
languages in a format that all of the participating languages can work with. In contrast,
JavaScript objects can only be worked with directly through the JavaScript
programming language.
JSON - DataTypes
Number
Integer
1
Digits 1-9, 0 and positive or negative
Fraction
2
Fractions like .3, .9
Exponent
3
Exponent like e, e+, e-, E, E+, E-
Syntax
Example
String
Sr.No. Type & Description
"
1
double quotation
\
2
backslash
/
3
forward slash
b
4
backspace
f
5
form feed
n
6
new line
r
7
carriage return
t
8
horizontal tab
u
9
four hexadecimal digits
Syntax
Example
Boolean
Syntax
Example
Array
Syntax
[ value, .......]
Example
Object
Syntax
Example
{
"id": "011A",
"language": "JAVA",
"price": 500,
}
Whitespace
It can be inserted between any pair of tokens. It can be added to make a code more
readable. Example shows declaration with and without whitespace −
Syntax
{string:" ",....}
Example
null
Syntax
null
Example
var i = null;
"employee": {
"name": "sachin",
"salary": 56000,
"married": true
}
}
In the above example, employee is an object in which "name", "salary" and "married"
are the key. In this example, there are string, number and boolean value for the keys.
{
"name": "sonoo",
"email": "[email protected]"
}
JSON supports numbers in double precision floating-point format. The number can be
digits (0-9), fractions (.33, .532 etc) and exponents (e, e+, e-,E, E+, E-).
{
"integer": 34,
"fraction": .2145,
"exponent": 6.61789e+0
}
{"employees":[
{"name":"Ram", "email":"[email protected]", "age":23},
{"name":"Virat", "email":"[email protected]", "age":33},
{"name":"Sachin", "email":"[email protected]", "age":43},
{"name":"Rahul", "email":"[email protected]", "age":41}
]}
[
[ "a", "b", "c" ],
[ "m", "n", "o" ],
[ "x", "y", "z" ]
]
JSON Comments
JSON doesn't support comments. It is not a standard.
But you can do some tricks such as adding extra attribute for comment in JSON object,
for example:
{
"employee": {
"name": "Sachin",
"salary": 56000,
"comments": "He is a nice man"
}
}
<html>
<head>
<title>Creating Object JSON with JavaScript</title>
<script language = "javascript" >
var JSONObj = { "name" : "Sachin Tendulkar", "age" : 50 };
<body>
</body>
</html>