Introduction to JSON Lecture22
Introduction to JSON Lecture22
JSON
Opening question…..
Data Types Strong support All values are Supports various Strong, typed
strings types schema
Parsing Speed Fast Slower Slower Very fast
{
"name" : "John",
"cars" : [ "Ford", "BMW",
"Fiat" ]
}
JSON Data Types
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
■JSON values cannot be one of the following data
types:
◆
a
◆
function
◆
a date
undefine
d
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.
■Example
{
"employee" : { "name" : "John", "age" : 30, "city" :
"New York" }
}
JSON Arrays
■Values in JSON can be arrays.
■Example
{
"employees" : [ "John", "Anna",
"Peter" ]
}
JSON Booleans
■Values in JSON can be true or
false.
■Example
{ "sale" : true }
JSON null
■Values in JSON can be
null.
■Example
{ "middlename" : null }
Nested JSON Objects
■Values in a JSON object can be another JSON
object.
■
Example
myObj =
{
"name":"John
", "age":30,
"cars" : {
"car1
":
"Ford
",
"car2
":
Arrays in JSON Objects
■Arrays can be values of an object
property:
■Example
myObj = {
"name" :
"John", "age" :
30,
"cars" : [
"Ford",
Nested Arrays in JSON Objects
■Values in an array can also be another array, or even another
JSON object:
■Example
myObj = {
"name" :
"John", "age" :
30,
"cars" : [
{ "nam
e" :
"Ford",
"model
s":
[ "Fiest
a",
"Focus"
,
"Musta
ng" ] },
{ "nam
e" :
"BMW",
"model
s" :
[ "320",
JSON Data Example
JSON vs XML
■Both JSON and XML can be used as a data
format when sending/receiving data
between servers.
■The following JSON and XML examples both
defines an employees object, with an array of
3 employees:
JSON vs XML
{"employees": [
{ "firstName":"John", "lastName":"Doe" },
{ "firstName":"Anna", "lastName":"Smith" },
{ "firstName":"Peter", "lastName":"Jones" }
]
}
<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>
JSON vs XML
■JSON is like XML
because:
◆
Both JSON and XML are "self
describing" (human readable)
◆
Both JSON and XML are hierarchical
(values within values)
◆
Both JSON and XML can be parsed and
used by
lots of programming languages
JSON vs XML
■JSON is unlike XML
because:
◆
JSON doesn't use end
◆
tag JSON is shorter
◆
JSON is quicker to read and
◆
write JSON can use arrays
JSON vs XML
■Since XML is widely used as data interchange
format, we will try to draw a comparison
between them. The purpose of the
comparison is not to determine which is
better but rather we will try to understand
which one is suitable for storing specific kind
of data.
JSON vs XML
■XML is more expressive than JSON. XML
sometimes also suffers from using tags
repeatedly, where as JSON is much more
concise.
■XML is more complex than JSON.
■JSON lacks namespaces
■There are several specifications to define
schema(metadata) for XML, for example
DTD and XSD. JSON schema is available but
is not as widely used as XML schemas.
JSON vs XML
■XML and JSON can be used with most of the programming
languages. The problem comes when matching XML tags
used by one system may be different in another system,
hence, XML data will require transformation.
i.e. <FirstName> vs <first_name>.
In the case of JSON, since objects and arrays are basic
data structures used, it is easy to work with them in
programs.
■For selecting specific parts of an XML document, there is
standard specification called XPath. This is widely used. In
JSON, we have JSONPath to do the same, but it is not
widely used.
JSON vs XML – Detailed Comparison
Feature JSON XML
Format Type Data-interchange format Markup language
Readability More concise and readable More verbose
Uses nested elements and
Data Structure Uses objects and arrays
attributes
Strong (via attributes and
Support for Metadata Limited
DTD/XSD)