5 - Json
5 - Json
{
"employee_id": 1234567,
"name": "Jeff Fox",
"hire_date": "1/1/2013",
"location": "Norwalk, CT",
"consultant": false
}
JSON Data – A name and a value
• In JSON, values must be one of the following data types:
• a string
• a number
• an object (JSON object)
• an array
• a boolean
• null
{
"employee_id": 1234567,
"name": "Jeff Fox",
"hire_date": "1/1/2013",
"location": "Norwalk, CT",
"consultant": false
}
JSON Data – A name and a value
• Strings in JSON must be written in double quotes.
{ "name":"John" }
{"employees":[
{ "firstName":"John", "lastName":"Doe" },
{ "firstName":"Anna", "lastName":"Smith" },
{ "firstName":"Peter", "lastName":"Jones" }
]}
Class Activity 8
• Convert the following bookstore.xml to bookstore.json
<?xml version="1.0"?>
<bookstore>
<book category="sci-fi">
<title lang="en"> 2001</title>
<author>Arthur C. Clarke</author>
<price>$30.0</price>
<year>1968</year>
</book>
<book>
<title lang="rs">Story about a True Man</title>
<author>Boris Polevoy</author>
<price>$20.00</price>
<year>1952</year>
</book>
7
</bookstore>
XML vs JSON
• 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
Using JSON
5. Fetch a JSON string.
6. Parse the JSON using JavaScript functions.