16.7. JSON Recap
JavaScript Object Notation
The most popular format for data exchange
JSON format is similar to
dictnotation in PythonFields are always enclosed only by double quote
"characterInstead of
Truethere istrue(lowercase)Instead of
Falsethere isfalse(lowercase)Instead of
Nonethere isnulllistis known asarray(despite the same syntax)JSON has no
tupleorsetComa
,is not allowed after the last element in list or objectObject of type
tuplewill serialize aslistObject of type
setis not JSON serializabledictis known asobject(despite the same syntax)Coma
,is not allowed after the last element in list or objectcamelCaseis convention, althoughsnake_caseis 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.toolto 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ść
# %% Expected
# >>> result
# [{"firstname": "Alice", "lastname": "Apricot", "age": 30},
# {"firstname": "Bob", "lastname": "Blackthorn", "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 has an is invalid version; expected: `3.9` or newer.'
>>> from os import remove
>>> result = open(FILE).read()
>>> remove(FILE)
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is str, \
'Variable `result` has an invalid type; expected: `str`.'
>>> assert len(result) > 0, \
'Variable `result` has an invalid length; expected more than zero elements.'
>>> from pprint import pprint
>>> pprint(result, width=79)
('[{"firstname": "Alice", "lastname": "Apricot", "age": 30}, {"firstname": '
'"Bob", "lastname": "Blackthorn", "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 -f -v myfile.py`
# %% Imports
import json
# %% Types
# %% Data
FILE = '_temporary.json'
DATA = [
('firstname', 'lastname', 'age'),
('Alice', 'Apricot', 30),
('Bob', 'Blackthorn', 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. Read data from the file `FILE`
# 2. Deserialize data from JSON format
# 3. Convert data from `list[dict]` to `list[tuple]
# 4. Use the `json` module
# 5. Run doctests - all must succeed
# %% Polish
# 1. Odczytaj dane z pliku `FILE`
# 2. Odserializuj dane z formatu JSON
# 3. Przekonwertuj dane z `list[dict]` to `list[tuple]`
# 4. Użyj modułu `json`
# 5. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> result
# [('age', 'firstname', 'lastname'),
# (30, 'Alice', 'Apricot'),
# (31, 'Bob', 'Blackthorn'),
# (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 has an is invalid version; expected: `3.9` or newer.'
>>> from os import remove
>>> remove(FILE)
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is list, \
'Variable `result` has an invalid type; expected: `list`.'
>>> assert len(result) > 0, \
'Variable `result` has an invalid length; expected more than zero elements.'
>>> assert all(type(x) is tuple for x in result), \
'Variable `result` has elements of an invalid type; all items should be: `tuple`.'
>>> from pprint import pprint
>>> pprint(result)
[('age', 'firstname', 'lastname'),
(30, 'Alice', 'Apricot'),
(31, 'Bob', 'Blackthorn'),
(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 -f -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": "Blackthorn", "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ść
# %% Expected
# >>> 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 has an is invalid version; expected: `3.9` or newer.'
>>> from os import remove
>>> remove(FILE)
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is list, \
'Variable `result` has an invalid type; expected: `list`.'
>>> assert len(result) > 0, \
'Variable `result` has an invalid length; expected more than zero elements.'
>>> assert all(type(x) is tuple for x in result), \
'Variable `result` has elements of an invalid type; all items should be: `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 -f -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 = ...