Understanding JSON: Syntax, Structure and Uses

4 minute
642 words
Categories:
Computer Science
Tags:

What is JSON

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It was derived from the ECMAScript programming language, but is programming language independent. JSON defines a small set of structuring rules for the portable representation of structured data.

JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

Structure of JSON

JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

Data Strucutres in JSON

A data structure is a method of organizing, storing, and managing data in a computer so that it can be accessed and modified efficiently.

A JSON data structure is built using these main components:

1. Object

An object is a collection of data stored in name and value pairs.It starts with a left curly bracket { and ends with a right curly bracket }.

In an object:

  • A name and value are separated by a colon :
  • Different pairs are separated by a comma ,
JSON
1
{
2
  "country": "India",
3
  "capital": "Delhi"
4
}

2. Array

An array is a collection of multiple values stored in order. It starts with a left square bracket [ and ends with a right square bracket ].

In an array:

  • Values are separated by a comma ,.
JSON
1
{
2
    "states" : ["Rajasthan", "Gujarat", "Andhra Pardesh"]
3
}

3. String

A string is a group of characters or text written inside double quotes "". It can contain letters, numbers, symbols, or spaces.

In JSON:

  • Strings are always written inside double quotes
  • A single character is also treated as a string
  • a backslash escape \ is used to include special characters inside a string.
JSON
1
{
2
  "country": "India",
3
  "capital": "Delhi"
4
}

4. Value

A value in JSON can be:

  • A string inside double quotes
  • A number
  • true or false
  • null
  • An object
  • An array

These data structures can also be placed inside one another, which is called nesting.

JSON
1
{
2
  "name": "Anurag",
3
  "age": 20,
4
  "isStudent": true,
5
  "skills": ["HTML", "CSS"],
6
  "address": {
7
    "city": "Jaipur"
8
  }
9
}

5. Number

A number in JSON is similar to a number in C or Java. It can be an integer or a decimal number.

However, JSON does not support:

  • Octal numbers
  • Hexadecimal numbers
JSON
1
{
2
  "age": 20,
3
  "price": 99.99
4
}

6. Whitespace

Whitespace means empty spaces, tabs, or new lines in JSON. It can be added between values and symbols to make the JSON easier to read. Whitespace does not change the meaning of JSON.

JSON
1
{
2
  "name": "Anurag",
3
  "age": 20,
4
  "isDeveloper": true,
5
  "skills": ["HTML", "CSS", "JavaScript"],
6
  "address": {
7
    "city": "Ajmer",
8
    "state": "Rajasthan"
9
  },
10
  "projects": [
11
    {
12
      "title": "Portfolio Website",
13
      "status": "Completed"
14
    },
15
    {
16
      "title": "Blog Website",
17
      "status": "In Progress"
18
    }
19
  ]
20
}

Uses of JSON

  1. Data Exchange - Used to send data between a server and a web application.

  2. APIs - Most APIs use JSON to transfer data.

  3. Web Applications - Used in frontend and backend development.

  4. Configuration Files - Many applications store settings in JSON format.

  5. Database Storage - Some databases store data in JSON form.

  6. Mobile Applications - Apps use JSON to receive and send data from servers.

Previous

Architecture of Intel 8085 Microprocessor

Next

How to create Multiline text underline Animation in CSS