JSON

Parse, Stringify

Zoom

Zoom recording - starts at 40:15

Link List

What is JSON?

JSON is an acronym for "JavaScript Object Notation". Any data sent to or from the server needs to be in a string format. The JSON format takes objects and represents them as strings so they can be sent back and forth.

Example of a String

Note the single quotes around the object. This object has been made into a string and can be passed through the server. Since the string contains double quotes, then single quotes have to be used to create the string to not confuse the server. Property names must also be double-quoted strings. There is no comma after the last key:value pair.

var menu = '{
    "javascript" : "javascript.html",
    "objects" : "JSObjects.html", 
    "JSON" : "json.html"
}';

In order to send the string or object back and forth between a browser and a server, it needs to be changed. To take a JSON string and turn it into an object we need to use JSON.parse(). To do the opposite and turn it into a string we need to use JSON.stringify().

JSON.parse()

Parsing the file Sample JSON will return the following object:

Code used:
var menu = JSON.parse(newMenu.responseText);
    
Returns:
{
    javascript : "javascript.html",
    objects : "javascript-objects.html",
    JSON : "json.html"
}

JSON.stringify()

Now that we have retrieved the JSON file and parsed it into an object. We can turn it back into a string with JSON.stringify().
code used: 
var stringifyObject = JSON.stringify(menu);

Returns:

*Due to long strings on mobile, please see console for actual outputs versus typed text displayed.