15.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"}
}

15.1.1. History

  • 1991 - Tim Berners-Lee creates first website

  • 1993 - Tim Berners-Lee publishes HTML specification

  • 1995 - Brendan Eich creates JavaScript

  • 1996 - iframe tag was introduced by Internet Explorer

  • 1998 - Microsoft Outlook Web Access team developed XMLHttpRequest

  • 2001 - Douglas Crockford creates JSON format

  • 2004 - Google introduces Gmail using XMLHttpRequest

  • 2005 - Yahoo! began offering some of its Web services in JSON

  • 2005 - Jesse James Garrett coins term AJAX

  • 2006 - W3C starts publishes XMLHttpRequest specification

  • 2013 - JSON was standardized as ECMA-404

  • 2017 - JSON was standardized as ISO/IEC 21778:2017

Comments were intentionally excluded from JSON. In 2012, Douglas Crockford described his design decision thus [1]:

I removed comments from JSON because I saw people were using them to hold parsing directives, a practice which would have destroyed interoperability.

JSON disallows "trailing commas", a comma after the last value inside a data structure. Trailing commas are a common feature of JSON derivatives to improve ease of use. [2], [3]

15.1.2. Int

  • Works the same way as in Python

Python:

1

JSON:

1

15.1.3. Float

  • Works the same way as in Python

Python:

2.0

JSON:

2.0

15.1.4. Bool

  • Instead of True there is true (lowercase)

  • Instead of False there is false (lowercase)

Python:

True
False

JSON:

true
false

15.1.5. None

  • Instead of None there is null

Python:

None

JSON:

null

15.1.6. Str

  • Fields are always enclosed only by double quote " character

Python:

'Alice'
"Apricot"

JSON:

"Alice"
"Apricot"

15.1.7. 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]

15.1.8. 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]

15.1.9. 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"}

15.1.10. 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}

15.1.11. Unicode

Python:

'cześć'

JSON:

"cze\\u015b\\u0107"

15.1.12. 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.

15.1.13. 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)

15.1.14. 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": []},
]

15.1.15. References