Introduction
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 (JavaScript Object Notation) is a file format used in object-oriented
programming that uses human-readable language, text, and syntax to store and
communicate data objects between applications.
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.
JSON is built on two structures which are
An ordered list of values, which translates to arrays, vectors, lists, or
sequences.
A collection of name/value pairs; can be an object, record, hash table, etc.
The most common uses of JSON include:
It is used in writing JavaScript-based applications that have websites and
browser extensions as part of their features.
It is essential in the transfer of structured data across network connections.
It is used to draw up data from a server by web applications.
JSON data format is used to publish public data by web services.
It is used in migrating from one database to another.
Similarities between the json and XML.
o Self-describing: Both json and xml are self-describing as both xml data and json
data are human-readable text.
o Hierarchical: Both json and xml support hierarchical structure. Here hierarchical
means that the values within values.
o Data interchange format: JSON and XML can be used as data interchange
formats by many different programming languages.
o Parse: Both the formats can be easily parsed.
o Retrieve: Both formats can be retrieved by using HTTP requests. The methods
used for retrieving the data are GET, PUT, POST.
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.
It is extended from javascript. It is extended from SGML.
It is open source means that we do It is also open source.
not have to pay anything to use
JSON.
The object created in JSON has XML data does not have any type.
some type.
The data types supported by JSON XML data is in a string format.
are strings, numbers, Booleans, null,
array.
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.
XML file is larger. If we want to represent the
data in XML then it would create a larger file as
compared to JSON.
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.
File size is smaller as compared to File size is larger.
XML.
JSON is data-oriented. XML is document-oriented.
It is less secure than XML. It is more secure than JSON.
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.
In JSON, data is represented in name/value pairs separated by a comma. The curly
bracket contains the object and is separated from the name by a colon. Square brackets
hold arrays, and a comma separates the array from values.
“cricketer”: [
“Name”: “Sachin Tendulkar”,
“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
The following table shows the number types −
Sr.No. Type & Description
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
var json-object-name = { string : number_value, .......}
Example
Example showing Number Datatype, value should not be quoted −
var obj = {marks: 97}
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
var json-object-name = { string : "string value", .......}
Example
Example showing String Datatype −
var obj = {name: 'Amit'}
Boolean
It includes true or false values.
Syntax
var json-object-name = { string : true/false, .......}
Example
var obj = {name: 'Amit', marks: 97, distinction: true}
Array
Syntax
[ value, .......]
Example
Example showing array containing multiple objects −
{
"books": [
{ "language":"Java" , "edition":"second" },
{ "language":"C++" , "edition":"fifth" },
{ "language":"C" , "edition":"third" }
]
}
Object
Syntax
{ string : value, .......}
Example
Example showing Object −
{
"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
var obj1 = {"name": "Sachin Tendulkar"}
var obj2 = {"name": "SauravGanguly"}
null
It means empty type.
Syntax
null
Example
var i = null;
JSON objects(with string and Numbers))
JSON object holds key/value pair. Each key is represented as a string in JSON and value
can be of any type. The keys and values are separated by colon. Each key/value pair is
separated by comma.
The curly brace { represents JSON object.
Let's see an example of JSON object.
"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.
JSON Object with Strings
The string value must be enclosed within double quote.
{
"name": "sonoo",
"email": "sonoojaiswal1987@gmail.com"
}
JSON Object with Numbers
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
}
JSON Arrays and their examples :
JSON array represents ordered list of values. JSON array can store multiple values. It
can store string, number, boolean or object in JSON array.
In JSON array, values must be separated by comma.
The [ (square bracket) represents JSON array.
JSON Array of Strings
Let's see an example of JSON arrays storing string values.
["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
JSON Array of Numbers
Let's see an example of JSON arrays storing number values.
[12, 34, 56, 43, 95]
JSON Array of Booleans
Let's see an example of JSON arrays storing boolean values.
[true, true, false, false, true]
JSON Array of Objects
Let's see a simple JSON array example having 4 objects.
{"employees":[
{"name":"Ram", "email":"ram@gmail.com", "age":23},
{"name":"Virat", "email":"virat23@gmail.com", "age":33},
{"name":"Sachin", "email":"sachin@gmail.com", "age":43},
{"name":"Rahul", "email":"rahul@gmail.com", "age":41}
]}
JSON Multidimensional Array
We can store array inside JSON array, it is known as array of arrays or multidimensional
array.
[
[ "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"
}
}
Here, "comments" attribute can be treated as comment.
Creating Simple Objects
JSON objects can be created with JavaScript.
Creation of an empty Object −
var JSONObj = {};
Creation of a new Object −
var JSONObj = new Object();
<html>
<head>
<title>Creating Object JSON with JavaScript</title>
<script language = "javascript" >
var JSONObj = { "name" : "Sachin Tendulkar", "age" : 50 };
document.write("<h1>JSON with JavaScript example</h1>");
document.write("<br>");
document.write("<h3> Name = "+JSONObj.name+"</h3>");
document.write("<h3> Age = "+JSONObj.age+"</h3>");
</script>
</head>
<body>
</body>
</html>