Working with JSON — Getting Started

John Au-Yeung - Jan 25 '21 - - Dev Community

Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62

Subscribe to my email list now at http://jauyeung.net/subscribe/

JSON stands for JavaScript Object Notation.

It’s a popular data-interchange format that has many uses.

In this article, we’ll take a look at how to use JSON.

Data Interchange Format

JSON is a data-interchange format.

It’s an alternative to other data interchange formats like XML.

It lets us carry data from one place to another in a format that both parties can understand.

JSON is a data-interchange format that’s been agreed upon for communicating data.

JSON it’s a programming language-independent format.

It looks like JavaScript object literals but it can be created and parsed with any language.

JSON Syntax

The JSON syntax is based on JavaScript object literals.

For example, we can write:

{  
    "brand": "shoes",  
    "color": "pink",  
    "size": 9,  
    "hasLaces": false  
}
Enter fullscreen mode Exit fullscreen mode

We have key-value pairs listed in the curly braces.

The keys are always in double-quotes.

And the values are also in double-quotes if they’re strings.

Each key-value pair is separated by a comma.

This notation is based on JavaScript, but they don’t include the functionality of JavaScript object literals.

They can’t include functions and only some data types can be added.

The colon is used to separate keys and values, so we have:

"brand": "shoes"
Enter fullscreen mode Exit fullscreen mode

The key can also have spaces, so we can write:

"my animal": "cat"
Enter fullscreen mode Exit fullscreen mode

If we want multiple words in the key, it would be better to write:

"myAnimal": "cat"
Enter fullscreen mode Exit fullscreen mode

This way, we don’t have to use the quotes everywhere when we try to get the value by the key.

Now we just have to make it an object by wrapping the key-value pair with curly braces.

For example, we can write:

{ "animal" : "cat" }
Enter fullscreen mode Exit fullscreen mode

JSON objects can have multiple key-value pairs in it. They are separated by commas.

For instance, we can write:

{ "animal" : "cat", "color" : "white" }
Enter fullscreen mode Exit fullscreen mode

The rules for JSON is rigid since it’s made to be read by machines.

A JSON object should have all of the following characters:

  • { (left curly bracket) means begin object
  • } (right curly bracket) means end object
  • : (colon) — separate a key and value in a key-value pair

If a JSON object has arrays, it should also have:

  • [ (left square bracket) means begin array
  • ] (right square bracket) means end array

If a JSON has more than one key-value pair, then it should also have:

  • , (comma) means separating a name-value pair in an object, or separate values in an array

The double quotes in the key are required.

So we can’t write:

{  
  title : "title",  
  body : "body"  
}
Enter fullscreen mode Exit fullscreen mode

or:

{  
  'title' : 'title',  
  'body' : 'body'  
}
Enter fullscreen mode Exit fullscreen mode

But we can write:

{  
  "title" : "title",  
  "body" : "body"  
}
Enter fullscreen mode Exit fullscreen mode

to create a JSON object.

Conclusion

JSON is a popular data-interchange format that we can use to communicate data.

It has its own syntax rules that are close to JavaScript object literals.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player