JSON Flashcards
What does JSON stand for?
JavaScript Object Notation
List the properties that make up the following JSON string:
'{"name":"John", "age":30, "car":null}'
- name
- age
- car
Note that each property has a corresponding value
How would you access the data from the following JSON string as an object?
'{"name":"John", "age":30, "car":null}'
Ex:
~~~
let personName = obj.name;
let personAge = obj.age;
~~~
List some reasons why you would use JSON:
- Format of JSON is syntactically similar to code for creating Javascript objects, so Javascript programs can easily convert JSON data into Javascript objects
- JSON format is text only (utilizes TUI’s, or text based user interfaces, meaning no graphics are used), so JSON can easily be sent between computers and used by any programming language
- Hence, JSON makes it possible to store Javascript objects as text
What is the built in Javascript function for converting JSON strings into Javascript objects?
JSON.parse()
What is the built in function for converting and object into a JSON string?
JSON.stringify()
JSON syntax is a _____ of Javascript syntax.
subset
What are basic syntax rules in JSON? (4 things)
- Data is in name/value pairs
- Data is separated by commas
- Curly braces hold objects
- Square brackets hold arrays
JSON data is written as ______/_______ pairs.
name/value pairs, or key/value pairs
What do name/value pairs consist of in JSON?
A field name (in double quotes), followed by a colon, followed by a value:
"name":"John"
(note JSON names and keys require double quotes)
List the data types that can be used as values in JSON:
- a string
- a number
- an object
- an array
- a boolean
- null
True or False:
String values must be written with double quotes in JSON.
True
Write a sample program in Javascript for an object that contains the following information:
* Properties: name, age, city
* Values: “John”, 31, “New York”
person = {name:"John", age:31, city:"New York"};
How would you access the name for this object?
person = {name:"John", age:31, city:"New York"};
//returns John person.name; //alternative way to access it person["name"];
How would you modify the name in this object to Gilbert?
person = {name:"John", age:31, city:"New York"};
person.name = "Gilbert"; //or person["name"] = "Gilbert";
How is JSON similar to XML? (4 ways)
- 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
- Both JSON and XML can be fetched with an XMLHttpRequest
How is JSON different than XML? (4 ways)
- JSON doesn’t use end tag
- JSON is shorter
- JSON is quicker to read and write
- JSON can use arrays
Why would you use JSON over XML?
- XML is much more difficult to parse than JSON.
- JSON is parsed into a ready-to-use JavaScript object.
Nam 3 INVALID data types that JSON does not accept:
- a function
- a date
- undefined
How would you write an object in JSON with the following properties:
* Object name: “employee”
* object properties: name: John, age: 30, city: New York
{ "employee":{"name":"John", "age":30, "city":"New York"} }
Are the following JSON examples valid or not:
1.
{ "employees":["John", "Anna", "Peter"] }
2.
{"sale":true}
3.
{"middlename":null}
- Valid
- Valid
- Valid
How is JSON used with web servers?
Used to exchange data to/from web servers.
(data received is always in string form)
At what point does the JSON data become a Javascript object?
Once the data is parsed with JSON.parse()
How would you parse the following JSON data and store it into an object called ‘obj’?
~~~
‘{“name”:”John”, “age”:30, “city”:”New York”}’
~~~
const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}');
(ensure the text you are parsing is in JSON format, or else it’ll error)