bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/JavaScript/Objects, Classes, and Advanced Patterns
JavaScript•Objects, Classes, and Advanced Patterns

JSON Syntax

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind JSON Syntax?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

"___":"John"
3Order

Put the learning moves in the order that makes the concept easiest to apply.

JavaScript Objects
JSON - Evaluates to JavaScript Objects
JSON Data - A Name and a Value

JSON syntax is derived from JavaScript object notation syntax:

  • Data is in name/value pairs
  • Data is separated by commas
  • Curly braces hold objects
  • Square brackets hold arrays

JSON Data - A Name and a Value

JSON data is written as name/value pairs (aka key/value pairs).

A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value:

Example

"name":"John"

JSON names require double quotes.

JSON - Evaluates to JavaScript Objects

The JSON format is almost identical to JavaScript objects.

In JSON, keys must be strings, written with double quotes:

Json

{"name":"John"}

In JavaScript, keys can be strings, numbers, or identifier names:

{name:"John"}

JSON Values

JSON Values

In JSON , values must be one of the following data types:

  • a string
  • a number
  • an object
  • an array
  • a boolean
  • null

In JavaScript values can be all of the above, plus any other valid JavaScript expression, including:

  • a function
  • a date
  • undefined

In JSON, string values must be written with double quotes:

Json

{"name":"John"}

In JavaScript, you can write string values with double or single quotes:

{name:'John'}

JavaScript Objects

Because JSON syntax is derived from JavaScript object notation, very little extra software is needed to work with JSON within JavaScript.

With JavaScript you can create an object and assign data to it, like this:

Example

person = {name:"John", age:31, city:"New York"};

You can access a JavaScript object like this:

// returns John person.name;

It can also be accessed like this:

// returns John person["name"];

Data can be modified like this

person.name = "Gilbert";

It can also be modified like this:

person["name"] = "Gilbert";

You will learn how to convert JavaScript objects into JSON later in this tutorial.

JavaScript Arrays as JSON

The same way JavaScript objects can be written as JSON, JavaScript arrays can also be written as JSON.

You will learn more about objects and arrays later in this tutorial.

JSON Files

  • The file type for JSON files is ".json"
  • The MIME type for JSON text is "application/json"

Previous

JavaScript JSON

Next

JSON vs XML