0% found this document useful (0 votes)
2 views

Unit VI_3

XML (EXtensible Markup Language) is designed to describe data and is independent of software and hardware, unlike HTML which focuses on displaying data. XML documents are structured with user-defined tags and must adhere to specific syntax rules to be considered 'well-formed' and 'valid' when using Document Type Definitions (DTD) or XML Schemas. Key features of XML include case sensitivity, the necessity of closing tags, proper nesting of elements, and the ability to include attributes.

Uploaded by

Neha Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit VI_3

XML (EXtensible Markup Language) is designed to describe data and is independent of software and hardware, unlike HTML which focuses on displaying data. XML documents are structured with user-defined tags and must adhere to specific syntax rules to be considered 'well-formed' and 'valid' when using Document Type Definitions (DTD) or XML Schemas. Key features of XML include case sensitivity, the necessity of closing tags, proper nesting of elements, and the ability to include attributes.

Uploaded by

Neha Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 72

XML

 XML stands for EXtensible Markup Language.


 XML was designed to describe data.
 XML is a software- and hardware-independent tool for
carrying information.
 HTML was designed to display data.
 XML is a markup language much like HTML
 XML was designed to describe data, not to display data
 XML tags are not predefined. You must define your own
tags
 XML is designed to be self-descriptive
 XML is a W3C Recommendation
 XML documents are used to transfer data from one place to another
often over the Internet.
 XML Document Example
 <?xml version="1.0" encoding="UTF-8"?>
<note>
<to> Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

 The above example is a note to Tove, from Jani, stored as XML:


 The note above is quite self descriptive. It has sender and receiver
information, it also has a heading and a message body.
 But still, this XML document does not DO anything. It is just
information wrapped in tags. Someone must write a piece of software
to send, receive or display it.
XML Documents
What‘s in an XML document?
 Elements
 Attributes
 plus some other details
A Simple XML Document
<article>
<author>Gerhard Weikum</author>
<title>The Web in Ten Years</title>
<text>
<abstract>In order to evolve...</abstract>
<section number=“1” title=“Introduction”>
The <index>Web</index> provides the universal...
</section>
</text>
</article>
A Simple XML Document
<article> Freely definable tags
<author>Gerhard Weikum</author>
<title>The Web in Ten Years</title>
<text>
<abstract>In order to evolve...</abstract>
<section number=“1” title=“Introduction”>
The <index>Web</index> provides the universal...
</section>
</text>
</article>
A Simple XML Document
<article> Start Tag
<author>Gerhard Weikum</author>
<title>The Web in Ten Years</title>
<text>
<abstract>In order to evolve...</abstract>
<section number=“1” title=“Introduction”>
The <index>Web</index> provides the universal...
</section>
</text>
</article>
Content of
End Tag the Element
Element (Subelements
and/or Text)
 Standard Generalized Markup Language
(SGML)
The Difference Between XML and HTML
 XML is not a replacement for HTML.
 XML and HTML were designed with different goals:
 XML was designed to describe data, with focus on what
data is
 HTML was designed to display data, with focus on how
data looks
 HTML is about displaying information, while XML is
about carrying information
 HTML tags have a fixed meaning and browsers know what
it is.
 XML tags are different for different applications, and users
know what they mean.
 HTML tags are used for display.
 XML tags are used to describe documents and data.
Example of an HTML Document

<html>
<head><title>Example</title></head>.
<body>
<h1>This is an example of a page.</h1>
<h2>Some information goes here.</h2>
</body>
</html>
Example of an XML Document

<?xml version=“1.0”/>
<address>
<name>Alice Lee</name>
<email>[email protected]</email>
<phone>212-346-1234</phone>
<birthday>1985-03-22</birthday>
</address>
XML Syntax Rules
 All XML Elements Must Have a Closing Tag
 In HTML, some elements do not have a closing tag:
 <p>This is a paragraph.
<br>
 In XML, it is illegal to omit the closing tag.
 All elements must have a closing tag:
 <p>This is a paragraph.</p>
<br />

 XML Tags are Case Sensitive


 The tag <Letter> is different from the tag <letter>.
 Opening and closing tags must be written with the same case:
 <Message>This is incorrect</message>
<message>This is correct</message>
 XML Elements Must be Properly Nested
 In HTML, you might see improperly nested elements:
 <b><i>This text is bold and italic</b></i>
 In XML, all elements must be properly nested within each
other:
 <b><i>This text is bold and italic</i></b>

 XML Documents Must Have a Root Element


 XML documents must contain one element that is the parent of
all other elements.
 This element is called the root element.
 <root>
<child>
<subchild>.....</subchild>
</child>
</root>
 XML Attribute Values Must be Quoted
 XML elements can have attributes in name/value pairs just like
in HTML.
 In XML, the attribute values must always be quoted.

 INCORRECT:
 <note date=12/11/2007>
<to>Tove</to>
<from>Jani</from>
</note>

 CORRECT:
 <note date="12/11/2007">
<to>Tove</to>
<from>Jani</from>
</note>
 The error in the first document is that the date attribute in the
note element is not quoted.
 Entity References
 Some characters have a special meaning in XML.
 If you place a character like "<" inside an XML element, it will
generate an error because the parser interprets it as the start of a
new element.
 This will generate an XML error:
 <message>if salary < 1000 then</message>
 To avoid this error, replace the "<" character with an entity
reference:
 <message>if salary &lt; 1000 then</message>
 There are 5 pre-defined entity references in XML:

&lt; < less than

&gt; > greater than

&amp; & ampersand

&apos; ' apostrophe

&quot; " quotation mark


 Comments in XML
 The syntax for writing comments in XML is similar to that of
HTML.
 <!-- This is a comment -->

 White-space is Preserved in XML


 XML does not truncate multiple white-spaces in a document
(while HTML truncates multiple white-spaces to one single
white-space):

XML: Hello Tove


HTML: Hello Tove
 Well Formed XML

 XML documents that conform to


the syntax rules above are said to
be "Well Formed" XML documents.
 What is an XML Element?

 An XML element is everything from (including) the


element's start tag to (including) the element's end tag.
 An element can contain:
 other elements
 text
 attributes
 or a mix of all of the above...
 <bookstore>
<book category="CHILDREN">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
 In the example above, <bookstore> and <book> have element
contents, because they contain other elements.
 <book> also has an attribute (category="CHILDREN").
 <title>, <author>, <year>, and <price> have text
content because they contain text.
 Empty XML Elements
 An element with no content is said to be empty.
 In XML, you can indicate an empty element like this:
 <element></element>
 or you can use an empty tag, like this (this sort of element syntax is
called self-closing):
 <element />
 The two forms above produce identical results in an XML parser.

 XML Naming Rules


 XML elements must follow these naming rules:
 Element names are case-sensitive
 Element names must start with a letter or underscore
 Element names cannot start with the letters xml (or XML, or Xml, etc)
 Element names can contain letters, digits, hyphens, underscores, and
periods
 Element names cannot contain spaces
 Any name can be used, no words are reserved (except xml).
 XML Attributes
 XML elements can have attributes, just like HTML.
 Attributes provide additional information about an element.
 In HTML, attributes provide additional information about
elements:
 <img src="computer.gif">
<a href="demo.asp">
 Attributes often provide information that is not a part of the
data.
 XML Attributes Must be Quoted
 Attribute values must always be quoted.
 Either single or double quotes can be used.
 For a person's gender, the person element can be written
like this:
 <person gender="female">
 or like this:
 <person gender='female'>
 If the attribute value itself contains double quotes you can
use single quotes, like in this example:
 <gangster name='George "Shotgun" Ziegler'>
 or you can use character entities:
 <gangster name="George &quot;Shotgun&quot; Ziegler">
 Viewing XML Files
 Raw XML files can be viewed in all major browsers.
 Don't expect XML files to be displayed as HTML pages.
 Viewing XML Files
 <?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
 Look at the XML file above in your browser: note.xml
 Notice that an XML document will be displayed with
color-coded root and child elements.
 A plus (+) or minus sign (-) to the left of the elements can
be clicked to expand or collapse the element structure.
 XML documents do not carry information about how to
display the data.
 Since XML tags are "invented" by the author of the XML
document, browsers do not know if a tag like <table>
describes an HTML table or a dining table.
 Without any information about how to display the data,
most browsers will just display the XML document as it is.
 XML Document Types
 An XML document with correct syntax is called "Well Formed".
 A "Valid" XML document must also conform to a document
type definition.
 Valid XML Documents
 A "valid" XML document is not the same as a "well formed"
XML document.
 A "valid" XML document must be well formed. In addition it
must conform to a document type definition.
 Rules that defines the legal elements and attributes for XML
documents are called Document Type Definitions (DTD) or
XML Schemas.
 There are two different document type definitions that can be
used with XML:
 DTD - The original Document Type Definition
 XML Schema - An XML-based alternative to DTD
 When to Use a DTD/Schema?
 With a DTD, independent groups of people can agree to use a
standard DTD for interchanging data.
 Your application can use a standard DTD to verify that the data
you receive from the outside world is valid.
 You can also use a DTD to verify your own data.

 When to NOT to Use a DTD/Schema?


 XML does not require a DTD/Schema when you are
experimenting with XML, or when you are working with small
XML files, creating DTDs may be a waste of time.
 XML DTD
 An XML document with correct syntax is called "Well Formed".
 An XML document validated against a DTD is "Well Formed"
and "Valid".
 Valid XML Documents
 A "Valid" XML document is a "Well Formed" XML document,
which also conforms to the rules of a DTD:
 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "Note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
 The DOCTYPE declaration, in the example above, is a reference to an
external DTD file.
 The purpose of a DTD is to define the structure of an XML document.
It defines the structure with a list of legal elements:
 The content of the file is shown below.
 <!DOCTYPE note
[
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
 The DTD above is interpreted like this:
 !DOCTYPE note defines that the root element of the document is note
 !ELEMENT note defines that the note element must contain four elements:
"to, from, heading, body"
 !ELEMENT to defines the to element, from element ,heading element ,body
element to be of type "#PCDATA“
 #PCDATA means parse-able text data.
 Using DTD for Entity Declaration
 A doctype declaration can also be used to define special
characters and character strings, used in the document:
 Example
 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ENTITY nbsp "&#xA0;">
<!ENTITY writer "Writer: Donald Duck.">
<!ENTITY copyright "Copyright: W3Schools.">
]>

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
<footer>&writer;&nbsp;&copyright;</footer>
</note>
 Why Use a DTD?

 With a DTD, independent groups of people can agree on a


standard for interchanging data.
 With a DTD, you can verify that the data you receive from
the outside world is valid.
XML Schema
 An XML Schema describes the structure of an XML
document, just like a DTD.
 An XML document with correct syntax is called "Well
Formed".
 An XML document validated against an XML Schema is
both "Well Formed" and "Valid".
 XML Schema is an XML-based alternative to DTD:
 <xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
 The Schema above is interpreted like this:
 <xs:element name="note"> defines the element called
"note"
 <xs:complexType> the "note" element is a complex type
 <xs:sequence> the complex type is a sequence of elements
 <xs:element name="to" type="xs:string"> the element "to"
is of type string (text)
 <xs:element name="from" type="xs:string"> the element
"from" is of type string
 <xs:element name="heading" type="xs:string"> the
element "heading" is of type string
 <xs:element name="body" type="xs:string"> the element
"body" is of type string
XPath
 XPath is a syntax for defining parts of an XML document
 XPath uses path expressions to navigate in XML documents
 XPath contains a library of standard functions
 XPath is a major element in XSLT (Extensible Style sheet
Language Transformation)
 XSL(eXtensible Stylesheet Language) is styling language for
XML.
 With XSLT you can transform an XML document to HTML
 XPath is also used in XQuery, XPointer and XLink
 XPath is a W3C recommendation
 XPath Path Expressions

 XPath uses path expressions to select nodes or node-sets in


an XML document.
 These path expressions look very much like the
expressions you see when you work with a traditional
computer file system.
 Today XPath expressions can also be used in JavaScript,
Java, XML Schema, PHP, Python, C and C++, and lots of
other languages.
 XPath is a major element in the XSLT standard.
 Without XPath knowledge you will not be able to create
XSLT documents.
 XPath Example <book category="WEB">
 We will use the following XML document: <title lang="en">XQuery Kick
 <?xml version="1.0" encoding="UTF-8"?> Start</title>
<bookstore> <author>James McGovern</author>
<book category="COOKING"> <author>Per Bothner</author>
<title lang="en">Everyday Italian</title> <author>Kurt Cagle</author>
<author>Giada De Laurentiis</author> <author>James Linn</author>
<year>2005</year> <author>Vaidyanathan
<price>30.00</price> Nagarajan</author>
</book> <year>2003</year>
<book category="CHILDREN"> <price>49.99</price>
<title lang="en">Harry Potter</title> </book>
<author>J K. Rowling</author> <book category="WEB">
<year>2005</year> <title lang="en">Learning XML</title>
<price>29.99</price> <author>Erik T. Ray</author>
</book> <year>2003</year>
<price>39.95</price>
</book>
</bookstore>
 In the table below we have listed some XPath expressions and the
result of the expressions:
XPath Expression Result

/bookstore/book[1] Selects the first book element that is the child of the
bookstore element
/bookstore/book[last()] Selects the last book element that is the child of the
bookstore element
/bookstore/book[last()-1] Selects the last but one book element that is the child
of the bookstore element
/bookstore/book[position()<3] Selects the first two book elements that are children
of the bookstore element
//title[@lang] Selects all the title elements that have an attribute
named lang
//title[@lang='en'] Selects all the title elements that have a "lang"
attribute with a value of "en"
/bookstore/book[price>35.00] Selects all the book elements of the bookstore
element that have a price element with a value
greater than 35.00
/bookstore/book[price>35.00]/title Selects all the title elements of the book elements of
the bookstore element that have a price element with
a value greater than 35.00
XQuery
 XQuery is to XML what SQL is to database tables.
 XQuery is designed to query XML data - not just XML
files, but anything that can appear as XML, including
databases.

 XQuery Example

 for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
 What is Xquery
 XQuery is the language for querying XML data
 XQuery is built on XPath expressions
 XQuery is supported by all major databases
 XQuery is a W3C Recommendation
 XQuery is a language for finding and extracting elements
and attributes from XML documents.
 Here is an example of a question that XQuery could solve:
 "Select all CD records with a price less than $10 from the
CD collection stored in the XML document called
cd_catalog.xml“

 XQuery can be used to:


 Extract information to use in a Web Service
 Generate summary reports
 Transform XML data to XHTML
 Search Web documents for relevant information
XQuery Example
 "books.xml":
 <?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
 XQuery uses functions to extract data from XML documents.
 The doc() function is used to open the "books.xml" file:
 doc("books.xml")
 XQuery uses path expressions to navigate through elements in an
XML document.
 The following path expression is used to select all the title elements in
the "books.xml" file:
 doc("books.xml")/bookstore/book/title
 (/bookstore selects the bookstore element, /book selects all the book
elements under the bookstore element, and /title selects all the title
elements under each book element)
 The XQuery above will extract the following:
 <title lang="en">Everyday Italian</title>
<title lang="en">Harry Potter</title>
<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>
 XQuery uses predicates to limit the extracted data from
XML documents.
 The following predicate is used to select all the book
elements under the bookstore element that have a price
element with a value that is less than 30:
 doc("books.xml")/bookstore/book[price<30]

 The XQuery above will extract the following:

 <book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
XQuery FLWOR Expressions
 How to Select Nodes From "books.xml" With FLWOR
 Look at the following path expression:
 doc("books.xml")/bookstore/book[price>30]/title
 The expression above will select all the title elements
under the book elements that are under the bookstore
element that have a price element with a value that is
higher than 30.
 The following FLWOR expression will select exactly the
same as the path expression above:
 for $x in doc("books.xml")/bookstore/book
where $x/price>30
return $x/title
 The result will be:
 <title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>
 With FLWOR you can sort the result:
 for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
 FLWOR is an acronym for "For, Let, Where, Order by, Return".
 The for clause selects all book elements under the bookstore element
into a variable called $x.
 The where clause selects only book elements with a price element
with a value greater than 30.
 The order by clause defines the sort-order. Will be sort by the title
element.
 The return clause specifies what should be returned. Here it returns
the title elements.
 The result of the XQuery expression above will be:
 <title lang="en">Learning XML</title>
<title lang="en">XQuery Kick Start</title>
JSON: Overview
 JSON or JavaScript Object Notation is a lightweight text-
based open standard designed for human-readable data
interchange.
 Conventions used by JSON are known to programmers
which include C, C++, Java, Python, Perl etc.
 JSON stands for JavaScript Object Notation.
 This format was specified by Douglas Crockford.
 This was designed for human-readable data interchange
 It has been extended from the JavaScript scripting
language.
 The filename extension is .json
 Uses of JSON

 It is used when writing JavaScript based application which includes


browser extension and websites.
 JSON format is used for serializing & transmitting structured data
over network connection.
 This is primarily used to transmit data between server and web
application.
 Web Services and API.s use JSON format to provide public data.
 It can be used with modern programming languages.

 Characteristics of JSON
 Easy to read and write JSON.
 Lightweight text based interchange format
 Language independent.
Simple Example in JSON
 Example shows Books information stored using JSON considering
language of books and their editions:

 { "book": [
 { "id":"01",
 "language": "Java",
 "edition": "third",
 "author": "Herbert Schildt" },
 { "id":"07",
 "language": "C++",
 "edition": "second"
 "author": "E.Balagurusamy"
 }
 ]
 }
 After understanding the above program we will try another example,
let's save the below code as json.htm:
 <html>
 <head>
 <title>JSON example</title>
 <script language="javascript" >
 var object1 = { "language" : "Java", "author" : "herbert schildt" };
 document.write("<h1>JSON with JavaScript example</h1>");
 document.write("<br>");
 document.write("<h3>Language = " + object1.language+"</h3>");
 document.write("<h3>Author = " + object1.author+"</h3>");
 var object2 = { "language" : "C++", "author" : "E-Balagurusamy" };
 document.write("<br>");
 document.write("<h3>Language = " + object2.language+"</h3>");
document.write("<h3>Author = " + object2.author+"</h3>");
document.write("<hr />");
 document.write(object2.language + " programming language can be studied "
+ "from book written by " + object2.author); document.write("<hr />");
 </script>
 </head><body></body>
 </html>
 Now let's try to open json.htm using IE or any other javascript enabled
browser, this produces the following result
JSON - Syntax
 Let's have a quick look on JSON basic syntax. JSON syntax is
basically considered as subset of JavaScript syntax, it includes the
following:
 Data is represented in name/value pairs
 Curly braces hold objects and each name is followed by ':'(colon), the
name/value pairs are separated by , (comma).
 Square brackets hold arrays and values are separated by ,(comma).
 Below is a simple example:
 { "book": [
 { "id":"01",
 "language": "Java",
 "edition": "third",
 "author": "Herbert Schildt“
 },
 { "id":"07",
 "language": "C++",
 "edition": "second"
 "author": "E.Balagurusamy"
 }
 ]
 }
 JSON supports following two data structures:
 Collection of name/value pairs: This Data Structure is supported by
different programming language.
 Ordered list of values: It includes array, list, vector or sequence etc.
JSON - DataTypes
 There are following datatypes supported by JSON format:

Type Description
double- precision floating-point
Number
format in JavaScript
double-quoted Unicode with
String
backslash escaping
Boolean true or false
Array an ordered sequence of values
it can be a string, a number, true
Value
or false, null etc
an unordered collection of
Object
key:value pairs
can be used between any pair of
Whitespace
tokens
null empty
 Number
 It is a double precision floating-point format in JavaScript and it
depends on implementation.
 Octal and hexadecimal formats are not used.
 The following table shows number types:
Type Description
Integer Digits 1-9, 0 and positive or negative
Fraction Fractions like .3, .9
Exponent 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
 It is a sequence of zero or more double quoted Unicode characters
with backslash escaping.
 Character is a single character string i.e. a string with length 1.
 The table shows string types:
Type Description
" double quotation
\ reverse solidus
/ solidus
b backspace
f form feed
n new line
r carriage return
t horizontal tab
u 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

 It is an ordered collection of values.


 These are enclosed square brackets which means that array begins
with .[. and ends with .]..
 The values are separated by ,(comma).
 Array indexing can be started at 0 or 1.
 Arrays should be used when the key names are sequential integers.

 Array Syntax:
 [ value, .......]

 Example:

 Example showing array containing multiple objects:


 { "books": [ { "language":"Java" , "edition":"second" },
 { "language":"C++" , "lastName":"fifth" },
 { "language":"C" , "lastName":"third" } ]
 }
 Object
 It is an unordered set of name/value pairs.
 Object are enclosed in curly braces that is it starts with '{' and ends
with '}'.
 Each name is followed by ':'(colon) and the name/value pairs are
separated by , (comma).
 The keys must be strings and should be different from each other.
 Objects should be used when the key names are arbitrary strings

 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
code more readable. Example shows declaration with and without
whitespace:
 Syntax:
 {string:" ",....}
 Example:
 var i= " sachin";
 var j = " saurav"
 null
 It means empty type.
 Syntax:
 null
 Example:
 var i = null;
 if(i==1)
 {
 document.write("<h1>value is 1</h1>");
 }
 Else
 { document.write("<h1>value is null</h1>");
 }
 JSON Value
 It includes:
 number (integer or floating point)
 string
 boolean
 array
 object
 null

 Syntax:

 String | Number | Object | Array | TRUE | FALSE | NULL

 Example:
 var i =1;
 var j = "sachin";
 var k = null;
JSON - Objects
 Creating Simple Objects

 JSON objects can be created with Javascript.


 Let us see various ways of creating JSON objects using Javascript:
 Creation of an empty Object:
 var JSONObj = {};
 Creation of new Object:
 var JSONObj = new Object();
 Creation of an object with attribute bookname with value in string,
attribute price with numeric value.
 Attributes is accessed by using '.' Operator :
 var JSONObj = { "bookname ":"VB BLACK BOOK", "price":500 };
 This is an example which shows creation of an object in javascript using
JSON, save the below code as json_object.htm:
 <html>
 <head>
 <title>Creating Object JSON with JavaScript</title>
 <script language="javascript" >
 var JSONObj = { "name" : "tutorialspoint.com", "year" : 2005 };
 document.write("<h1>JSON with JavaScript example</h1>");
 document.write("<br>");
 document.write("<h3>WebsiteName="+JSONObj.name+"</h3>");
document.write("<h3>Year="+JSONObj.year+"</h3>");
 </script>
 </head>
 <body>
 </body>
 </html>
 Now let's try to open json_object.htm using IE or any other
javascript enabled browser, this produces the following
result:
 Creating Array Objects
 Below example shows creation of an array object in javascript using
JSON, save the below code as json_array_object.htm:
 <html>
 <head>
 <title>Creation of array object in javascript using JSON</title>
 <script language="javascript" >
 document.writeln("<h2>JSON array object</h2>");
 var books = { "Pascal" : [ { "Name" : "Pascal Made Simple", "price" : 700 },
{ "Name" : "Guide to Pascal", "price" : 400 } ],
 "Scala" : [ { "Name" : "Scala for the Impatient", "price" :
1000 },
 { "Name" : "Scala in Depth", "price" : 1300 } ] }
 var i = 0
 document.writeln("<table border='2'><tr>");
 for(i=0;i<books.Pascal.length;i++)
 { document.writeln("<td>");
 document.writeln("<table border='1' width=100 >");
 document.writeln("<tr><td><b>Name</b></td><td width=50>" +
books.Pascal[i].Name+"</td></tr>");
document.writeln("<tr><td><b>Price</b></td><td width=50>" +
books.Pascal[i].price +"</td></tr>");
 document.writeln("</table>"); document.writeln("</td>");}
 for(i=0;i<books.Scala.length;i++)
 {
 document.writeln("<td>");
 document.writeln("<table border='1' width=100 >");
 document.writeln("<tr><td><b>Name</b></td><td width=50>" +
books.Scala[i].Name+"</td></tr>");
 document.writeln("<tr><td><b>Price</b></td><td width=50>" +
books.Scala[i].price+"</td></tr>");
 document.writeln("</table>");
 document.writeln("</td>");}
 document.writeln("</tr></table>");
 </script></head><body></body></html>
 Now let's try to open json_array_object.htm using IE or any other
javascript enabled browser, this produces the following result:
JSON - Schema
 JSON Schema is a specification for JSON based format for
defining structure of JSON data.
 It was written under IETF draft which expired in 2011.

 JSON Schema:
 Describes your existing data format.
 Clear, human- and machine-readable documentation.
 Complete structural validation, useful for automated
testing.
 Complete structural validation, validating client-submitted
data.
 JSON Schema Example
 Following is a basic JSON schema which covers a classical product
catalog description:
 { "$schema": "http://json-schema.org/draft-04/schema#",
 "title": "Product",
 "description": "A product from Acme's catalog",
 "type": "object",
 "properties": {
 "id": { "description": "The unique identifier for a product",
 "type": "integer"
 },
 "name": {
 "description": "Name of the product",
 "type": "string"
 },
 "price": {
 "type": "number",
 "minimum": 0,
 "exclusiveMinimum": true
 }
 }, "required": ["id", "name", "price"]
 }
Keywords Description
The $schema keyword states that this schema is written according to the draft v4
$schema
specification.
title You will use this to give a title to your schema
description A little description of the schema
The type keyword defines the first constraint on our JSON data: it has to be a JSON
type
Object.
Defines various keys and their value types, minimum and maximum values to be used in
properties
JSON file.
required This keeps a list of required properties.
minimum This is the constraint to be put on the value and represents minimum acceptable value.

If "exclusiveMinimum" is present and has boolean value true, the instance is valid if it is
exclusiveMinimum
strictly greater than the value of "minimum".

maximum This is the constraint to be put on the value and represents maximum acceptable value.

If "exclusiveMaximum" is present and has boolean value true, the instance is valid if it is
exclusiveMaximum
strictly lower than the value of "maximum".

A numeric instance is valid against "multipleOf" if the result of the division of the instance
multipleOf
by this keyword's value is an integer.

maxLength The length of a string instance is defined as the maximum number of its characters.

minLength The length of a string instance is defined as the minimum number of its characters.
A string instance is considered valid if the regular expression matches the instance
pattern
successfully.
 Above schema can be used to test the validity of the below
given JSON code:
 [
 { "id": 2,
 "name": "An ice sculpture",
 "price": 12.50,
 },
 { "id": 3,
 "name": "A blue mouse",
 "price": 25.50,
 }
 ]
JSON - Comparison with XML
 JSON and XML are human readable formats and are language
independent.
 They both have support for creation, reading and decoding in real
world situations.
 We can compare JSON with XML based on the following factors:

 Verbose
 XML is more verbose (Expressed in more words than needed) than
JSON, so it's faster to write JSON for humans.
 Arrays Usage
 XML is used to describe structured data which doesn't include arrays
whereas JSON include arrays.
 Parsing
 JavaScript's eval method parses JSON. When applied to JSON, eval
returns the described object.
 Example
 This shows individual examples of XML and JSON:

 JSON

 { "company": Volkswagen, "name": "Vento", "price": 800000}

 XML

 <car>
 <company>Volkswagen</company>
 <name>Vento</name>
 <price>800000</price>
 </car>

You might also like