Submodule 13.3: Object Notation

Site: ΕΛ/ΛΑΚ Moodle
Course: Study / Web Design and Web Development
Book: Submodule 13.3: Object Notation
Printed by: Guest user
Date: Thursday, 21 November 2024, 11:16 PM

Description

  • What is JSON?
  • JSON structure
  • Arrays as JSON

What is JSON?

The initials of JSON stands for JavaScript Object Notation. 

Using JSON, we can store and transport data.

We use JSON in order to sent data from a server to a page or vice versa.

As you will see, the format of a JSON object is very similar to the one we are using to create JavaScript Objects.

JSON syntax

An example of a JSON object is:

{
  "dishes": [
    {
      "id": 0,
      "name": "85% Dark Chocolate",
      "image": "images/dark85.jpg",
      "category": "darks",
      "label": "Hot",
      "price": "4.99",
      "featured": "true",
      "description": "Dark chocolate with no added sugar. Here you can taste the flavor of cocoa beans."
   }
]
}

Based on the above, let's analyze the syntax :

  • They are surrounded by curly brackets { }
  • They have key( eg "id")-value( eg 0) pairs. 
  • Each key-value pair is separated from the next one with a comma
  • Each key is separated from its value by a colon
  • Each key must be a string and written with double quotes
  • Objects contain arrays ( in this case 1 array) which should be surrounded by Square brackets 

In our example, the object "dishes" is an array. It contains 1 object.

Examples - Exercices

Example 1

Exercise

  1. Suppose that we want to have the results "Interdimensional travel" and 2016.  Which are the appropriate commands for these results?

Answer:

superHeroes['members'][2]['powers'][4]
superHeroes.formed
Example 2

Exercise

  1. Suppose that we want to have the results "Damage resistance" and "Madame Uppercut".  Which are the appropriate commands for these results?

Answer:

superHeroes[1].powers[1]
superHeroes[1].name

See more about JSON