JSON example in JavaScript

by Julien Etienne

JavaScript

// This is the JSON data as a string, 
// it's the same as JSON in a file except without the surrounding ``.
const data = `{
		"movies" : [
			"Superman",
			"Spiderman",
      "Wonder Woman",
      "Batman"
		],

     "shows":{
        "gameOfThrones": "Game of Thrones",
        "friends": "Friends",
        "siliconValley": "Silicon Valley"
     }
}`;

// We can't use it as a string, we need to convert it into 
// an object using JSON.parse.
const dataAsObject = JSON.parse(data);


/*
 Here we can display parts of the data.
 You need to open up the developer tools console to see the results
 Uncomment each line below to see more
*/
console.group('View Data');
console.log('This is the data:', dataAsObject);
// console.log('All movies:', dataAsObject.movies);
// console.log('All shows:', dataAsObject.shows);
// console.log('Game of Thrones:', dataAsObject.shows.gameOfThrones);
// console.log('Game of Thrones bracket notation:', dataAsObject.shows["gameOfThrones"]);
console.groupEnd();