16.1. JSON About
JavaScript Object Notation
The most popular format for data exchange
JSON format is similar to
dictnotation 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
Truethere istrue(lowercase)Instead of
Falsethere isfalse(lowercase)
Python:
True
False
JSON:
true
false
16.1.4. None
Instead of
Nonethere isnull
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
listis known asarray(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
tupleObject of type
tuplewill serialize aslistComa
,is not allowed after the last element in list or object
Python:
(1, 2, 3)
JSON:
[1, 2, 3]
16.1.8. Dict
dictis known asobject(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
camelCaseis convention, althoughsnake_caseis also validFields 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 jsonto prettify (humanize) outputBefore Python 3.14 - use
python -m json.toolto prettify (humanize) outputpython -m json.toolis deprecated in favor ofpython -m jsonSince Python 3.14 -
python -m jsonhas 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": []},
]