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";