Animations

API

Arrays

Async

Basics

Challenges

Classes

Console

Dates

Debugging

DOM Elements

DOM Methods

DOM Navigation

DOM Properties

Event Listeners

Flow Control

Forms

Functions

Global Functions

JSON

Keywords

Libraries (3rd party)

Math

Modules

Objects

Snippets

String

Types

Widgets

Window Object

JavaScript - Basics

JSON is a format for storing and transporting data. JSON is often used when data is sent from a server to a web page. JSON stands for JavaScript Object Notation. Simply put, JSON files are just a collection of key : value pairs. The key is a string and the value can be a simple data type, or another list or object.

Objects in JSON are surrounded by curly braces:

The JSON format evaluates to multiple JavaScript Objects.

The JSON format is almost syntactically identical to the code for creating JavaScript objects. For example, here's how we create an object called "person":

var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};

With a JSON Object, we are essentially creating an ARRAY of person objects and calling it PERSONS.


{
    "persons":
    [
        {"firstName":"John", "lastName":"Doe", age:50, eyeColor:"blue"},
        {"firstName":"Anna","lastName":"Smith", age:40, eyeColor:"blue"},
        {"firstName":"Peter", "lastName":"Jones", age:30, eyeColor:"blue"}
    ]
}

JSON Syntax Rules:

  • Data is in name/value pairs
  • Data is separated by commas
  • Curly braces hold objects
  • Square brackets hold arrays
  • JSON names/attributes/properties (firstName) require double quotes. JavaScript object names/attributes/properties do not.
  • a JSON Object can just be an single object or it can be an array of objects.

 

See the difference?


//object
var person = { firstName:"John", lastName:"Doe", age:50, eyeColor:"blue" };

//json object
{
    "persons":
    [
        { "firstName":"John", "lastName":"Doe", age:50, eyeColor:"blue" },
        { "firstName":"John", "lastName":"Doe", age:50, eyeColor:"blue" }
    ]
}