Patricia Sauer - Authentic Programming

What is JSON?

Screen with Javascript on it

JSON stands for JavaScript Object Notation. It is a data format which needs way less space than XML. This keeps the needed traffic of web applications low and therefore JSON is used extensively in web applications.

What is an object in JavaScript?

So what is an object in JavaScript? In JavaScript an object

  • is enclosed by curly braces ({ })
  • contains variables which can then be
    • primitive types,
    • objects, or
    • arrays which can then contain objects again

In my opinion, having a look at an example is the best way to understand this:

{ 
  user: 'learner', 
  notes: [ 
    { 
      content: 'this is my first note', 
      date: 1532724600000 
    }, 
    { 
      content: 'this is my second note', 
      date: 1532724600635 
    } 
  ] 
}

In this example we have an object which is related to notes. A user learner created two notes. Here, user is a variable of type string. The notes of the user are stored in an array which consists of two note objects. Each note object has an element content which represents the content of the note and an element date which represents the timestamp when the note was created in milliseconds.

What is a valid JSON?

JSON is not much different from an object in JavaScript. The only difference is that the variable names, i.e. user, notes, content, and date have to be handled as strings and therefore need to be surrounded by quotations marks. In JSON, you should use double quotes for the keys and values which represent strings.

Let’s have a look at what the example above looks in JSON:

{ 
  "user": "learner", 
  "notes": [ 
    { 
      "content":"this is my first note",
      "date":1532724600000 
    }, 
    { 
      "content":"this is my second note",
      "date":1532724600635 
    } 
  ] 
}

That is everything you need to know about the JavaScript Object Notation. All you need now is a little practice and with time you will be able to read JSON easily.

Liked this article?

Buy Me A Coffee

 
© 2018 - 2024 Patricia Sauer