Intro to JSON

  • JSON stands for JavaScript Object Notation.
  • JSON is a data format that stores information in key:value pairs, just like a JavaScript Object does
  • In other words, JSON is a specific syntax of JavaScript that is used to serialize certain data types


  • JSON is commonly used to store and exchange data between a server and web application or browser.



  • For example, when you visit a website and login with your username and password, you’re submitting data to a server that checks your login credentials and either logs you into the website, or tells you the login information was incorrect.
  • In order for the browser and server to communicate and actually log you in or send an error message, the information you entered must be submitted to the server. This is where JSON becomes useful.


  • JSON may be stored in its own file with the file extension .json, or put directly into code when it is stored as a string
  • Because JSON uses JavaScript notation, any JavaScript object can be converted into JSON and sent to a server.
  • Likewise, a server can return JSON data to us, and the JSON data can be converted to JavaScript objects and used as such.


  • JSON syntax is derived from JavaScript object syntax.
  • Remember that JavaScript objects are stored in key:value pairs (if you need a refresher on objects, review lesson 1.3 JS Data Types)
  • JSON syntax also requires that data is stored in name:value (AKA key:value) pairs.


  • These syntax rules are similar to the syntax rules for JavaScript objects, however there are a few notable differences:
    • JSON name/value pairs both have to be wrapped in double quotation marks (” “). This rule excludes number values, booleans, and null.


{"species":"cat"}, {"age": 21}  JS object: {species: "cat"} or {species: 'cat'}, {age: 21}


  • A JSON object has to be stored in a variable as a string, which is then parsed into JS.
  • The JSON object is stored in a string by placing the entirety of the JSON data within a pair of single quotes: ‘ ’
  • For example, here is JSON data storing student info:
{ "firstName" : "Millie", "lastName" : "Brown", "grade" : 10 }

JSON diffs

  • A JavaScript object can contain attributes of practically any data type, including a date or a function, as we practiced in earlier lessons
  • JSON objects, however, cannot have attributes that are of the following types:
    • functions
    • dates
    • undefined


  • Which means that JSON objects can have attributes of the following data types:
    • strings
    • numbers
    • other JSON objects
    • arrays
    • booleans
    • null

Accessing JSON

  • Dot Notation: values of the JSON object can be obtained by specifying the objectName.keyName like so:
dogObj = {"breed":"poodle", "name":"Moxie", "age": 3, "likes":"fluffy toys"};

console.log( + " is a " + dogObj.age + " year old " + dogObj.breed);

Accessing JSON

  • Bracket Notation: values of the JSON object can be obtained by specifying the objectName[“keyName”] like so:
catObj = {"breed":"Siamese", "name":"ChiChi", "age": 2, "likes":"napping"};
console.log(catObj["name"] + " is a " + catObj["age"] + " year old " + catObj["breed"] + "cat");

Modifying JSON

  • Dot and Bracket notation are also used to modify values of the JSON object:
    • Dot notation modification:
catObj.age = 2.5;

Modifying JSON

  • Bracket notation modification:
dogObj["likes"] = "tennis balls";
  • this is useful if your key is a number