JSON Files Flashcards
JSON Stands for
JavaScript Object Notation and was inspired by JavaScript
Advantages of JSON
-Easy to read and write
-Lange independent
-Same syntax as JavaScript Objects
-Store application data
When exchanging data between a browser and a server
data can be text, but JSON can be easily converted into objects
Jason Uses 2 Structures
Name, Value pairs,
which in many languages can be referred to as an object,
struct, or dictionary
An ordered list of values
which in many languages this is referred to as an array or
list
What extension do JSON files have
.json extension
JSON Values
JSON values can be the following:
Strings: Enclosed in double quotes
{“name”: “Gina”}
Numbers
{“age”: 29}
True or False
{“allergies”: true}
Null
{“faxNumber”: null}
Objects
{
“patient”: {“name”: “Gina”, “age”: 29, “city”: “Columbus”}
}
Arrays
{
“patients”: [“Gina”, “Rick”, “ed”, “Russ”]
}
JSON Objects
These structures can also be nested. JSON data types cannot be a function, date, or undefined. JSON objects are name/value pairs separated by colons. Pairs are separated by commas and enclosed by curly braces.
{
“storeName”: “Starlight cafe”,
“owner”: “Fred Smith”
}
Arrays are enclosed by square brackets and separated by commas.
“items”: [“banana”, “coffee”, “muffin”]
Arrays can be anything including a collections of objects (nested).
JavaScript has a built-in function that converts
a string into a JavaScript Object
JSON.parse()
When receiving _____ from a ______, it can be used as any JavaScript object. All objects are written as Key/Value pairs and Keys must be ______.
data
server
strings
Nesting JSON objects (books 1-4 are nested in books)
myObj = (
“name”: “Gina”,
“age”: 29,
“books”: {
“book1”: “Harry Potter”,
“book2”: “Percy Jackson”,
“book3”: Thrawn”,
“book4”: “Mistborn”
}
}
JSON Exceptions
Dates and functions are not allowed but they can be written as strings and converted later
Date to Convert to a String
var strPerson = ‘ {“name”: “Gina”, “birthday”: “1992-07-05”, “state”: “OH”}’;
var obj = JSON.parse(strPerson);
Parsing Functions
var strPerson = ‘{ “name”: “Gina”, “age”: “function () {return 29;}”, “state”: “OH”}’ ;
var obj = JSON.parse(strPerson) ;
JSON Versus XML
JSON
-Easy to read and understand
-Hierarchical
-No tags needed
-Easy to use with JavaScript
-Lightweight and fast
-Can use arrays
XML
-Easy to read and understand
-Hierarchical
-Requires an XML parser
-Supports more complex data types such as datetime
-Supports namespace