JavaScript Object Notation(JSON)

Theory

JSON (or JavaScript Object Notation) is a text-based format for storing and transmitting structured data. It comes from the JavaScript language, but it is still considered to be language-independent: it works with almost any programming language. With JSON's lightweight syntax, you can easily store and send to other apps everything from numbers and strings to arrays and objects. You can also create more complex data structures by linking arrays to each other.

  1. Basic syntax and structure

JSON text can be built on one of two structures:

  • a collection of key:value pairs (associative array);

  • an orderly set of values (array or list).

JSON objects are written in curly braces {}, and their key:value pairs are separated by a comma ,. The key and the value in the pair are separated by a colon :. Here is an example for you:

{
    "first_name": "Avyan",
    "last_name": "Sharma",
    "age": 22
}

Arrays are written in square brackets [] and their values are separated by a comma ,. The value in the array, again, can be of any type, including another array or object. Here is an example of an array:

["night", "street", false, [ 345, 23, 8, "juice"], "fruit"]

Most often, an array will include similar elements.

JSON does not support comments.

  1. Nested objects

JSON is a highly flexible format. You can nest objects inside other objects as properties:

{
  "persons": [
    {
      "firstName": "Suraj ",
      "lastName": "Sharma",
      "age": 20
    },
    {
      "firstName": "Avyan ",
      "lastName": "Singh",
      "age": 26
    },
    {
      "firstName": "Ritanshu",
      "lastName": "Sharma",
      "age": 34
    }
  ]
}

If objects and arrays contain other objects or arrays, the data has a tree-like structure.

The nested objects are fully independent and may have different properties:

{
  "persons": [
    {
      "firstName": "Avyan",
      "age": 20
    },
    {
      "firstName": "Raj",
      "lastName": "Sharma"
    }
  ]
}
  1. camelCase VS snake_case

If you have read the JSON objects examples really carefully, you might have a lingering question: what style of compound word writing should be used for JSON?

CamelCase is a style where compound words are written together and without spaces, but each word inside the phrase starts with a capital letter. The style is called camelCase because the capital letters inside the word resemble camel's humps.

In snake_case style, compound words are written through the bottom underline.

  1. The advantages of JSON

JSON is widely spread for data exchange on the Internet because of its strong advantages:

  • compactness;

  • flexibility;

  • high readability, even for people far from programming;

  • most programming languages have functions and libraries for reading and creating JSON structures.

The JSON is a general format to pass structured data through the network because after you serialize data to JSON, you can deserialize it back without losing any information. The main advantage of JSON comparing to plain text is the ability to describe relations between objects via nesting and key-value pairs. So, it's high chances that the sites you're often visiting use JSON too.

Other popular applications of JSON are data storage and configuration files for other programs.

  1. Conclusion

Now you have seen that JSON is easy to understand and use, and it's quite awesome since it's a very useful tool for transferring data between applications. In working practice, you probably won't have to create JSON files yourself, you will get them from other sources, but if you want to save the code on your computer, you should save the files in the .json extension.