JSON Syntax and Structure

In the previous guide, we introduced the basic concepts of JSON and its application in data exchange. Now, we will delve into the syntax and structure of JSON so that you can use this format more effectively to organize and exchange data.

Basics of JSON Syntax

The syntax of JSON is very concise, based on two main structures: objects and arrays.

Objects

A JSON object is an unordered collection of key-value pairs. Each key is unique and associated with a value. Objects are enclosed in curly braces {} in JSON.

Example:

{
  "title": "The Great Gatsby",
  "author": "F. Scott Fitzgerald",
  "year": 1925
}

In this example, title, author, and year are keys, and their respective values are "The Great Gatsby", "F. Scott Fitzgerald", and 1925.

Arrays

A JSON array is an ordered collection of values. Arrays are enclosed in square brackets [] in JSON.

Example:

[
  {
    "title": "The Great Gatsby",
    "author": "F. Scott Fitzgerald",
    "year": 1925
  },
  {
    "title": "1984",
    "author": "George Orwell",
    "year": 1949
  },
  {
    "title": "To Kill a Mockingbird",
    "author": "Harper Lee",
    "year": 1960
  }
]

In this example, the array contains three string values.

Key-Value Pairs

In a JSON object, each key-value pair is separated by a colon : followed by its value. Keys must be strings and are enclosed in double quotes ". Values can be strings, numbers, objects, arrays, boolean values (true or false), null, or one of the special values.

Example:

{
  "id": 1,
  "name": "T",
  "isActive": true,
  "tags": ["AI", "Assistant", "JSON"],
  "address": null
}

Data Types

JSON supports the following data types:

  1. String: Must be enclosed in double quotes.
  2. Number: Can be an integer or a floating-point number.
  3. Object: A collection of key-value pairs enclosed in curly braces.
  4. Array: A collection of values enclosed in square brackets.
  5. Boolean: true or false.
  6. null: Represents a null value.

A full example like this below

{
  "nullValue": null,
  "booleanValue": true,
  "numberValue": 123,
  "stringValue": "Hello, World!",
  "arrayValue": [1, 2, 3, "four", true, null],
  "objectValue": {
    "nestedNumber": 456,
    "nestedString": "Nested string",
    "nestedBoolean": false
  },
  "arrayOfObjects": [
    {
      "id": 1,
      "name": "Alice"
    },
    {
      "id": 2,
      "name": "Bob"
    }
  ],
  "objectWithArrays": {
    "tags": ["json", "demo", "example"],
    "permissions": [
      {
        "user": "admin",
        "access": "read-write"
      },
      {
        "user": "guest",
        "access": "read-only"
      }
    ]
  }
}

Nested Objects and Arrays

JSON allows objects and arrays to be nested within other objects or arrays, making JSON very suitable for representing complex data structures.

Example:

{
  "library": {
    "books": [
      {
        "title": "The Great Gatsby",
        "author": "F. Scott Fitzgerald",
        "year": 1925
      },
      {
        "title": "1984",
        "author": "George Orwell",
        "year": 1949
      },
      {
        "title": "To Kill a Mockingbird",
        "author": "Harper Lee",
        "year": 1960
      }
    ],   
  }
}

In this example, books is an array that contains multiple objects, each representing information about a book.

Formatting and Comments

  • Formatting: Although JSON does not require specific formatting, it is often indented and line-broken for improved readability.
  • Comments: JSON itself does not support comments. If you need to add comments, you can do so manually before parsing JSON, or use certain tools to add comments after parsing.

Conclusion

Through this guide, you should have a deeper understanding of JSON's syntax and structure. Understanding how to use objects and arrays to organize data.