16.7. JSON Recap
JavaScript Object Notation
The most popular format for data exchange
JSON format is similar to
dict
notation in PythonFields are always enclosed only by double quote
"
characterInstead of
True
there istrue
(lowercase)Instead of
False
there isfalse
(lowercase)Instead of
None
there isnull
list
is known asarray
(despite the same syntax)JSON has no
tuple
orset
Coma
,
is not allowed after the last element in list or objectObject of type
tuple
will serialize aslist
Object of type
set
is not JSON serializabledict
is known asobject
(despite the same syntax)Coma
,
is not allowed after the last element in list or objectcamelCase
is convention, althoughsnake_case
is also validFields are always enclosed only by double quote
"
characterUnicode characters are stored as unicode entities (
"cze\\u015b\\u0107"
)JSON can be minified to save space for network transmission
Minified JSON is not human readable
Use
python -m json.tool
to prettify (humanize) output
16.7.1. Assignments
# %% About
# - Name: JSON File Dump
# - Difficulty: easy
# - Lines: 3
# - Minutes: 5
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Serialize data from variable `DATA`
# 2. Convert data from `list[tuple]` to `list[dict]`
# 3. Use `json` module
# 4. Result write to file `FILE`
# 5. Run doctests - all must succeed
# %% Polish
# 1. Zserializuj dane ze zmiennej `DATA`
# 2. Przekonwertuj dane z `list[tuple]` to `list[dict]`
# 3. Użyj modułu `json`
# 4. Wynik zapisz do pliku `FILE`
# 5. Uruchom doctesty - wszystkie muszą się powieść
# %% Example
# >>> result
# [{"firstname": "Alice", "lastname": "Apricot", "age": 30},
# {"firstname": "Bob", "lastname": "Banana", "age": 31},
# {"firstname": "Carol", "lastname": "Corn", "age": 32},
# {"firstname": "Dave", "lastname": "Durian", "age": 33},
# {"firstname": "Eve", "lastname": "Elderberry", "age": 34},
# {"firstname": "Mallory", "lastname": "Melon", "age": 15}]
# %% Hints
# - `with open(mode='wt') as file:`
# - `json.dump()`
# - `dict(zip(header,row))`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from os import remove
>>> result = open(FILE).read()
>>> remove(FILE)
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is str, \
'Variable `result` has invalid type, should be str'
>>> assert len(result) > 0, \
'Variable `result` should not be empty'
>>> from pprint import pprint
>>> pprint(result, width=79)
('[{"firstname": "Alice", "lastname": "Apricot", "age": 30}, {"firstname": '
'"Bob", "lastname": "Banana", "age": 31}, {"firstname": "Carol", "lastname": '
'"Corn", "age": 32}, {"firstname": "Dave", "lastname": "Durian", "age": 33}, '
'{"firstname": "Eve", "lastname": "Elderberry", "age": 34}, {"firstname": '
'"Mallory", "lastname": "Melon", "age": 15}]')
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% Imports
import json
# %% Types
# %% Data
FILE = '_temporary.json'
DATA = [
('firstname', 'lastname', 'age'),
('Alice', 'Apricot', 30),
('Bob', 'Banana', 31),
('Carol', 'Corn', 32),
('Dave', 'Durian', 33),
('Eve', 'Elderberry', 34),
('Mallory', 'Melon', 15),
]
header, *rows = DATA
# %% Result
# %% About
# - Name: JSON Recap Load
# - Difficulty: easy
# - Lines: 2
# - Minutes: 5
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be user for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Serialize data from variable `DATA`
# 2. Convert data from `list[dict]` to `list[tuple]`
# 3. Add sorted header as a first line
# 4. Use `json` module
# 5. Result write to file `FILE`
# 6. Run doctests - all must succeed
# %% Polish
# 1. Zserializuj dane ze zmiennej `DATA`
# 2. Przekonwertuj dane z `list[dict]` to `list[tuple]`
# 3. Add posortowany nagłówek jako pierwszą linię
# 4. Użyj modułu `json`
# 5. Wynik zapisz do pliku `FILE`
# 6. Uruchom doctesty - wszystkie muszą się powieść
# %% Example
# >>> result
# [('age', 'firstname', 'lastname'),
# (30, 'Alice', 'Apricot'),
# (31, 'Bob', 'Banana'),
# (32, 'Carol', 'Corn'),
# (33, 'Dave', 'Durian'),
# (34, 'Eve', 'Elderberry'),
# (15, 'Mallory', 'Melon')]
# %% Hints
# - `with open(mode='rt') as file:`
# - `json.load()`
# - `dict.keys()`
# - `dict.values()`
# - `tuple()`
# - `list.append()`
# - `list.extend()`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from os import remove
>>> remove(FILE)
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is list, \
'Variable `result` has invalid type, should be list'
>>> assert len(result) > 0, \
'Variable `result` should not be empty'
>>> assert all(type(row) is tuple for row in result), \
'Variable `result` should be a list[tuple]'
>>> from pprint import pprint
>>> pprint(result)
[('age', 'firstname', 'lastname'),
(30, 'Alice', 'Apricot'),
(31, 'Bob', 'Banana'),
(32, 'Carol', 'Corn'),
(33, 'Dave', 'Durian'),
(34, 'Eve', 'Elderberry'),
(15, 'Mallory', 'Melon')]
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% Imports
import json
# %% Types
result: list[tuple]
# %% Data
FILE = r'_temporary.json'
with open(FILE, mode='wt') as file:
file.write("""[
{"firstname": "Alice", "lastname": "Apricot", "age": 30},
{"firstname": "Bob", "lastname": "Banana", "age": 31},
{"firstname": "Carol", "lastname": "Corn", "age": 32},
{"firstname": "Dave", "lastname": "Durian", "age": 33},
{"firstname": "Eve", "lastname": "Elderberry", "age": 34},
{"firstname": "Mallory", "lastname": "Melon", "age": 15}
]""")
# %% Result
result = ...
# %% About
# - Name: JSON Recap Blanks
# - Difficulty: medium
# - Lines: 13
# - Minutes: 13
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Serialize data from variable `DATA`
# 2. Convert data from `list[dict]` to `list[tuple]`
# 3. Add sorted header as a first line
# 4. Fill missing data with value `None`
# 5. Use `json` module
# 6. Result write to file `FILE`
# 7. Run doctests - all must succeed
# %% Polish
# 1. Zserializuj dane ze zmiennej `DATA`
# 2. Przekonwertuj dane z `list[dict]` to `list[tuple]`
# 3. Add posortowany nagłówek jako pierwszą linię
# 4. Wypełnij brakujące dane wartością `None`
# 5. Użyj modułu `json`
# 6. Wynik zapisz do pliku `FILE`
# 7. Uruchom doctesty - wszystkie muszą się powieść
# %% Example
# >>> result
# [('age', 'firstname', 'lastname'),
# (None, 'Alice', 'Apricot'),
# (31, 'Bob', None),
# (32, None, 'Corn'),
# (None, 'Dave', 'Durian'),
# (34, 'Eve', None),
# (None, 'Mallory', None)]
# %% Hints
# - `with open(mode='rt') as file:`
# - `json.load()`
# - `dict.keys()`
# - `dict.values()`
# - `dict.get(default=None)`
# - `dict comprehension`
# - `tuple()`
# - `set()`
# - `set.update()`
# - `set.add()`
# - `sorted()`
# - `list.append()`
# - `list.extend()`
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from os import remove
>>> remove(FILE)
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is list, \
'Variable `result` has invalid type, should be list'
>>> assert len(result) > 0, \
'Variable `result` should not be empty'
>>> assert all(type(row) is tuple for row in result), \
'Variable `result` should be a list[tuple]'
>>> from pprint import pprint
>>> pprint(result)
[('age', 'firstname', 'lastname'),
(None, 'Alice', 'Apricot'),
(31, 'Bob', None),
(32, None, 'Corn'),
(None, 'Dave', 'Durian'),
(34, 'Eve', None),
(None, 'Mallory', None)]
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% Imports
import json
# %% Types
result: list[tuple]
# %% Data
FILE = r'_temporary.json'
with open(FILE, mode='wt') as file:
file.write("""[
{"firstname": "Alice", "lastname": "Apricot"},
{"firstname": "Bob", "age": 31},
{"lastname": "Corn", "age": 32},
{"firstname": "Dave", "lastname": "Durian"},
{"firstname": "Eve", "age": 34},
{"firstname": "Mallory"}
]""")
# %% Result
result = ...