JavaScript

JSON Syntax | Explained

JSON is a standard for transferring data between different applications most commonly used in online servers to send and receive data to and from the client. JSON is based on JavaScript’s object syntax that is exactly why it is called the JavaScript Object Notation, however, the actual syntax of working with JSON is slightly different from the normal JavaScript.

In this post, we are going to discuss the rules of JSON syntax, the different values that can be used inside the JSON and their syntax as well and the file type of JSON file. So, let’s get started:

JSON Syntax rules

The syntax of the JSON is based on JavaScript object, however, it can be considered a subset of the JavaScript’s object’s syntax but not the same The difference comes from the rules of the syntax for JSON and these rules are as follows:

  • The data inside the JSON can only be present as “key-value” pairs
  • The key-value pairs are always separated by a colon
  • Each data element or key-value pair must be separated by a comma
  • To use objects inside the value of the key-value use curly brackets
  • To use arrays inside the value field of the key-value pair use the square bracket

Example

{
  "Name": " John Doe",
  "age": 25,
  "job": "Auditor"
}

JSON Data | Explained

Now that we are familiar with the JSON syntax rules, let’s have a look at the JSON data. As mentioned in the rules as well, the data inside the JSON format can only be written in “key-value” pairs. These key-value pairs are often called the name-value pairs as well.

Data consists of a key that is always encapsulated with double quotation marks followed by a colon “:” and then the value field of the data. Remember: the value field has its syntax which we are going to cover as well.

Example of JSON Data

An example of JSON data is:

{

  "city": "New York"

}

Another example would be:

{

  "Team": "Lakers"

}

JSON Data Syntax VS JavaScript Object Syntax

In JSON, the key of the key-value pair can only be a string value encapsulated with double quotation marks, wherein JavaScript a string key-value can either be encapsulated with a single quotation mark or with a double quotation mark:

In JSON:

{

  "Bird": "Eagle"

}

In JavaScript:

{

  'Bird': "Eagle"

}

Moreover, in JavaScript object they key can be of any type strings, int, boolean and even an object can be a key of the data, like

var demo = {

    Bird: "Eagle",

};

Or:

var demo = {

    Bird: "Eagle",

};

JSON Values | Acceptable Type

In the JSON syntax, the value field of the key-value pair can be one of the following data types:

  • String
  • Integer
  • Null
  • Objects
  • Arrays
  • Boolean

String, as already mentioned above, are always encapsulated with double quotation marks, even if the string is being used inside an Array value.

For the data type of Arrays, the value field is encapsulated with square brackets, For example:

{
  "name": "John Doe",
  "age": 25,
  "Job": "Auditor",
  "Hobbies": ["Cricket", "Soccer", "Guitar", "Watching Netflix"]
}

An object in the value field is encapsulated with curly brackets. And you can even use nested arrays and objects, For example:

{
  "name": "Bruce Wayne",
  "Super Power": "Super Rich",
  "Cars Owned": [
   {
      "name": "Murciélago",
      "Model": "LP 640",
      "Status": "Crashed"
    },
    {
      "name": "Aventador",
      "Model": " LP 700-4",
      "Status": "Seems Driveable after the accident"
    }
  ]
}

JSON | File Type

JSON data is often placed in a specific file which is known as the JSON file, this JSON file has the extension of .json”. The media type of the file is “application/json”

Conclusion

JSON syntax is based on the object syntax of JavaScript and the main reason for this is that JSON was mapped on JavaSript’s objects. Even with the similarity of the two, there are some differences in syntax rules. In this post, we learned about JSON syntax rules and their subtle differences with the javascript syntax.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.