16.1. JSON About

  • JavaScript Object Notation

  • The most popular format for data exchange

  • JSON format is similar to dict notation in Python

Python:

{
    'firstname': 'Alice',
    'lastname': 'Apricot',
    'height': 170,
    'weight': 55.5,
    'is_staff': True,
    'is_admin': False,
    'friends': None,
    'groups': ('users', 'staff'),
    'project': {'id': 1, 'name': 'myproject'}
}

JSON:

{
    "firstname": "Alice",
    "lastname": "Apricot",
    "height": 170,
    "weight": 55.5,
    "isStaff": true,
    "isAdmin": false,
    "friends": null,
    "groups": ["users", "staff"],
    "project": {"id": 1, "name": "myproject"}
}

16.1.1. Int

  • Works the same way as in Python

Python:

1

JSON:

1

16.1.2. Float

  • Works the same way as in Python

Python:

2.0

JSON:

2.0

16.1.3. Bool

  • Instead of True there is true (lowercase)

  • Instead of False there is false (lowercase)

Python:

True
False

JSON:

true
false

16.1.4. None

  • Instead of None there is null

Python:

None

JSON:

null

16.1.5. Str

  • Fields are always enclosed only by double quote " character

Python:

'Alice'
"Apricot"

JSON:

"Alice"
"Apricot"

16.1.6. List

  • list is known as array (despite the same syntax)

  • Works the same way as in Python

  • Coma , is not allowed after the last element in list or object

Python:

[1, 2, 3]

JSON:

[1, 2, 3]

16.1.7. Tuple

  • JSON has no tuple

  • Object of type tuple will serialize as list

  • Coma , is not allowed after the last element in list or object

Python:

(1, 2, 3)

JSON:

[1, 2, 3]

16.1.8. Dict

  • dict is known as object (despite the same syntax)

  • Works the same way as in Python

  • Coma , is not allowed after the last element in list or object

Python:

{'firstname': 'Alice', 'lastname': 'Apricot'}

JSON:

{"firstname": "Alice", "lastname": "Apricot"}

16.1.9. Identifiers

  • camelCase is convention, although snake_case is also valid

  • Fields are always enclosed only by double quote " character

Python:

{'is_admin': True}

JSON:

{"isAdmin": true}

16.1.10. Unicode

  • Unicode characters are stored as unicode entities ("cze\\u015b\\u0107")

Python:

'cześć'

JSON:

"cze\\u015b\\u0107"

16.1.11. JSON or Python?

Python:

{
    "database": "myapp",
    "table": "users",
    "rows": [
        {"username": "alice", "email": "alice@example.com"},
        {"username": "bob", "email": "bob@example.com"},
        {"username": "carol", "email": "carol@example.com"},
        {"username": "dave", "email": "dave@example.org"},
        {"username": "eve", "email": "eve@example.org"},
        {"username": "mallory", "email": "mallory@example.net"},
    ]
}

JSON:

{
    "database": "myapp",
    "table": "users",
    "rows": [
        {"username": "alice", "email": "alice@example.com"},
        {"username": "bob", "email": "bob@example.com"},
        {"username": "carol", "email": "carol@example.com"},
        {"username": "dave", "email": "dave@example.org"},
        {"username": "eve", "email": "eve@example.org"},
        {"username": "mallory", "email": "mallory@example.net"},
    ]
}

Both are identical.

16.1.12. Pretty Printing JSON

  • JSON can be minified to save space for network transmission

  • Minified JSON is not human readable

  • Since Python 3.14 - use python -m json to prettify (humanize) output

  • Before Python 3.14 - use python -m json.tool to prettify (humanize) output

  • python -m json.tool is deprecated in favor of python -m json

  • Since Python 3.14 - python -m json has colorized output

Minified JSON file:

$ curl https://python3.info/_static/users.json
[{"__type__":"User","firstname":"Alice","lastname":"Apricot","age":30,
"groups":[{"__type__":"Group","name":"users"},{"__type__":"Group",
"name":"staff"}]},{"__type__":"User","firstname":"Bob",
"lastname":"Blackthorn","age":31,"groups":[{"__type__":"Group",
"name":"users"},{"__type__":"Group","name":"staff"}]},{"__type__":"User",
"firstname":"Carol","lastname":"Corn","age":32,
"groups":[{"__type__":"Group","name":"users"}]},{"__type__":"User",
"firstname":"Dave","lastname":"Durian","age":33,
"groups":[{"__type__":"Group","name":"users"}]},{"__type__":"User",
"firstname":"Eve","lastname":"Elderberry","age":34,
"groups":[{"__type__":"Group","name":"users"},{"__type__":"Group",
"name":"staff"},{"__type__":"Group","name":"admins"}]},{"__type__":"User",
"firstname":"Mallory","lastname":"Melon","age":15,"groups":[]}]

Pretty Printing JSON:

$ curl https://python3.info/_static/users.json |python -m json
[
  {
    "__type__": "User",
    "firstname": "Alice",
    "lastname": "Apricot",
    "age": 30,
    "groups": [
      {
        "__type__": "Group",
        "name": "users"
      },
      {
        "__type__": "Group",
        "name": "staff"
      }
    ]
  },
  {
    "__type__": "User",
    "firstname": "Bob",
    "lastname": "Blackthorn",
    "age": 31,
    "groups": [
      {
        "__type__": "Group",
        "name": "users"
      },
      {
        "__type__": "Group",
        "name": "staff"
      }
    ]
  },
  {
    "__type__": "User",
    "firstname": "Carol",
    "lastname": "Corn",
    "age": 32,
    "groups": [
      {
        "__type__": "Group",
        "name": "users"
      }
    ]
  },
  {
    "__type__": "User",
    "firstname": "Dave",
    "lastname": "Durian",
    "age": 33,
    "groups": [
      {
        "__type__": "Group",
        "name": "users"
      }
    ]
  },
  {
    "__type__": "User",
    "firstname": "Eve",
    "lastname": "Elderberry",
    "age": 34,
    "groups": [
      {
        "__type__": "Group",
        "name": "users"
      },
      {
        "__type__": "Group",
        "name": "staff"
      },
      {
        "__type__": "Group",
        "name": "admins"
      }
    ]
  },
  {
    "__type__": "User",
    "firstname": "Mallory",
    "lastname": "Melon",
    "age": 15,
    "groups": []
  }
]

json.tool checks JSON syntax validity:

$ echo '{"firstname": "Mark", "lastname": "Watney",}' | python -m json.tool
Illegal trailing comma before end of object: line 1 column 43 (char 42)

16.1.13. Use Case - 1

[
    {"firstname": "Alice", "lastname": "Apricot", "groups": [
        {"gid": 1, "name": "users"},
        {"gid": 2, "name": "staff"},
    ]},

    {"firstname": "Bob", "lastname": "Blackthorn", "groups": [
        {"gid": 1, "name": "users"},
        {"gid": 2, "name": "staff"},
    ]},

    {"firstname": "Carol", "lastname": "Corn", "groups": [
        {"gid": 1, "name": "users"},
    ]},

    {"firstname": "Dave", "lastname": "Durian", "groups": [
        {"gid": 1, "name": "users"},
    ]},

    {"firstname": "Eve", "lastname": "Elderberry", "groups": [
        {"gid": 1, "name": "users"},
        {"gid": 2, "name": "staff"},
        {"gid": 3, "name": "admins"},
    ]},

    {"firstname": "Mallory", "lastname": "Melon", "groups": []},
]