Post

JSON: Basic Concepts & Data Types

JSON: Basic Concepts & Data Types

In this post you’ll learn what JSON is, why it’s so widely used, what its core syntax rules are, and what data types JSON supports. You’ll also see examples to cement your understanding.

What is JSON

  • JSON stands for JavaScript Object Notation 1.

  • It’s a lightweight, text-based format for representing structured data, independent of programming language 2.

  • It’s very popular for data interchange—APIs 3, configuration files 4, or anywhere you need a simple, readable format to move data between systems.

In JSON:

  • Data is represented as key:value pairs (in objects) or as ordered lists (in arrays).

  • Keys (names) must always be strings, enclosed in double quotes.

  • Whitespace (spaces, newlines, tabs) is allowed and ignored outside string literals, for readability.

  • JSON does not support comments.

JSON Syntax / Structure

  1. Object
    • Enclosed in curly braces { … }.
    • Inside: zero or more name/value pairs, separated by commas.
    • Name (aka key) must be a string in double-quotes.
    • The name and value are separated by a colon :.
    • Example:

      1
      2
      3
      4
      
        {
        "name": "Alice",
        "age": 30
        }
      
  2. Array
    • Enclosed in square brackets [ … ].
    • Elements are ordered, separated by commas.
    • Elements can be any valid JSON value (string, number, object, array, boolean, null)
    • Example:

      1
      2
      3
      
        {
        "colors": ["red", "green", "blue"]
        }
      
  3. Nesting
    • Objects and arrays can be nested arbitrarily.
    • You can have an object inside an array, array inside object, etc.
    • Example:

      1
      2
      3
      4
      5
      6
      7
      
        {
            "user": {
                "id": 1,
                "roles": ["admin", "user"]
            },
            "active": true
        }
      
  4. Whitespace and formatting
    • Whitespaces (spaces, tabs, line breaks) are allowed between tokens, to improve readability.
    • No trailing commas (i.e. you cannot have an extra comma before closing } or ]).

    • JSON does not support comments (// or /**/ are invalid) 5.

Data Types Supported in JSON

JSON supports six basic types (4 primitive, 2 compound) 6 :

TypeDescription / ConstraintsExample(s)
StringSequence of Unicode characters, must be in double quotes. Supports escape sequences (e.g. ", \, \n) 7."hello", "Line\nBreak"
NumberA numeric literal (integer or floating-point). No special values like NaN or Infinity allowed.123, 3.14, -42, 1.5e3
Booleantrue or falsetrue, false
NullThe literal null, meaning “no value”null
ObjectA collection of name/value pairs (keys are strings).{ "a": 1, "b": "text" }
ArrayAn ordered list of values (any of the types)[1, "two", false, null, { "x": 5 }]

Important caveats / notes:

  • JSON does not support functions, special types like undefined, or date/time types natively. Dates are usually encoded as strings 8.

  • In JSON, there is no distinction between integer vs floating-point types — both are “number” type 9.

Full Combined Example

Here is a more realistic JSON example combining types, nesting, arrays, etc.:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
  "first_name": "John",
  "last_name": "Smith",
  "is_alive": true,
  "age": 27,
  "address": {
    "street_address": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postal_code": "10021-3100"
  },
  "phone_numbers": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "office",
      "number": "646 555-4567"
    }
  ],
  "children": [
    "Catherine",
    "Thomas",
    "Trevor"
  ],
  "spouse": null
}

This is exactly the example shown on Wikipedia’s JSON page 10.

This post is licensed under CC BY 4.0 by the author.