15.2. JSON Dump — Python
json.dumps(object) -> str- Python object to JSON stringjson.dump(object) -> file- Python object to JSON filefile extension
.json
15.2.1. SetUp
15.2.2. Sequence
>>> DATA = ('Alice', 'Apricot', 30) >>> >>> result = json.dumps(DATA) >>> >>> print(result) ["Alice", "Apricot", 30]
15.2.3. List of Sequences
>>> DATA = [ ... ('firstname', 'lastname', 'age'), ... ('Alice', 'Apricot', 30), ... ('Bob', 'Blackthorn', 31), ... ('Carol', 'Corn', 32), ... ('Dave', 'Durian', 33), ... ('Eve', 'Elderberry', 34), ... ('Mallory', 'Melon', 15), ... ] >>> >>> result = json.dumps(DATA) >>> >>> print(result) [["firstname", "lastname", "age"], ["Alice", "Apricot", 30], ["Bob", "Blackthorn", 31], ["Carol", "Corn", 32], ["Dave", "Durian", 33], ["Eve", "Elderberry", 34], ["Mallory", "Melon", 15]]
15.2.4. Mapping
>>> DATA = {'firstname': 'Alice', 'lastname': 'Apricot', 'age': 30} >>> >>> result = json.dumps(DATA) >>> >>> print(result) {"firstname": "Alice", "lastname": "Apricot", "age": 30}
15.2.5. List of Mappings
>>> DATA = [ ... {'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 = json.dumps(DATA) >>> >>> print(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}]
15.2.6. To File
json.dump(data, file) -> Nonefile extension
.json
def dump(obj: Any,
fp: IO[str],
*,
skipkeys: bool = ...,
ensure_ascii: bool = ...,
check_circular: bool = ...,
allow_nan: bool = ...,
cls: type[JSONEncoder] | None = ...,
indent: None | int | str = ...,
separators: tuple[str, str] | None = ...,
default: (Any) -> Any | None = ...,
sort_keys: bool = ...,
**kwds: Any) -> None
SetUp:
>>> DATA = {'firstname': 'Alice', 'lastname': 'Apricot', 'age': 30}
Usage:
>>> with open('/tmp/myfile.json', mode='wt') as file: ... json.dump(DATA, file)
Result:
>>> result = open('/tmp/myfile.json').read() >>> print(result) {"firstname": "Alice", "lastname": "Apricot", "age": 30}
15.2.7. Assignments
# %% About # - Name: JSON Dump Sequence # - Difficulty: easy # - Lines: 1 # - Minutes: 1 # %% 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. Use `json` module # 3. Result write to variable `result` # 4. Run doctests - all must succeed # %% Polish # 1. Zserializuj dane ze zmiennej `DATA` # 2. Użyj modułu `json` # 3. Wynik zapisz do zmiennej `result` # 4. Uruchom doctesty - wszystkie muszą się powieść # %% Expected # >>> result # ["Alice", "Apricot", 30] # %% Hints # - `json.dumps()` # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert 'result' in globals(), \ 'Variable `result` is not defined; assign result of your program to it.' >>> 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.' >>> print(result) ["Alice", "Apricot", 30] """ # %% 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: str # %% Data DATA = ('Alice', 'Apricot', 30) # %% Result result = ...
# %% About # - Name: JSON Dump ListSequence # - Difficulty: easy # - Lines: 1 # - Minutes: 1 # %% 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. Use `json` module # 3. Result write to variable `result` # 4. Run doctests - all must succeed # %% Polish # 1. Zserializuj dane ze zmiennej `DATA` # 2. Użyj modułu `json` # 3. Wynik zapisz do zmiennej `result` # 4. Uruchom doctesty - wszystkie muszą się powieść # %% Expected # >>> result # [["firstname", "lastname", "age"], # ["Alice", "Apricot", 30], # ["Bob", "Blackthorn", 31], # ["Carol", "Corn", 32], # ["Dave", "Durian", 33], # ["Eve", "Elderberry", 34], # ["Mallory", "Melon", 15]] # %% Hints # - `json.dumps()` # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert 'result' in globals(), \ 'Variable `result` is not defined; assign result of your program to it.' >>> 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.' >>> print(result) # doctest: +NORMALIZE_WHITESPACE [["firstname", "lastname", "age"], ["Alice", "Apricot", 30], ["Bob", "Blackthorn", 31], ["Carol", "Corn", 32], ["Dave", "Durian", 33], ["Eve", "Elderberry", 34], ["Mallory", "Melon", 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 result: str # %% Data DATA = [ ('firstname', 'lastname', 'age'), ('Alice', 'Apricot', 30), ('Bob', 'Blackthorn', 31), ('Carol', 'Corn', 32), ('Dave', 'Durian', 33), ('Eve', 'Elderberry', 34), ('Mallory', 'Melon', 15), ] # %% Result result = ...
# %% About # - Name: JSON Dump Mapping # - Difficulty: easy # - Lines: 1 # - Minutes: 1 # %% 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. Use `json` module # 3. Result write to variable `result` # 4. Run doctests - all must succeed # %% Polish # 1. Zserializuj dane ze zmiennej `DATA` # 2. Użyj modułu `json` # 3. Wynik zapisz do zmiennej `result` # 4. Uruchom doctesty - wszystkie muszą się powieść # %% Expected # >>> result # {"firstname": "Alice", "lastname": "Apricot", "age": 30} # %% Hints # - `json.dumps()` # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert 'result' in globals(), \ 'Variable `result` is not defined; assign result of your program to it.' >>> 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.' >>> print(result) {"firstname": "Alice", "lastname": "Apricot", "age": 30} """ # %% 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: str # %% Data DATA = {'firstname': 'Alice', 'lastname': 'Apricot', 'age': 30} # %% Result result = ...
# %% About # - Name: JSON Dump ListMapping # - Difficulty: easy # - Lines: 1 # - Minutes: 1 # %% 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. Use `json` module # 3. Result write to variable `result` # 4. Run doctests - all must succeed # %% Polish # 1. Zserializuj dane ze zmiennej `DATA` # 2. Użyj modułu `json` # 3. Wynik zapisz do zmiennej `result` # 4. 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 # - `json.dumps()` # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert 'result' in globals(), \ 'Variable `result` is not defined; assign result of your program to it.' >>> 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.' >>> print(result) # doctest: +NORMALIZE_WHITESPACE [{"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 result: str # %% Data DATA = [ {'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 Dump ToFile # - Difficulty: easy # - Lines: 2 # - Minutes: 2 # %% 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. Use `json` module # 3. Result write to file `FILE` # 4. Run doctests - all must succeed # %% Polish # 1. Zserializuj dane ze zmiennej `DATA` # 2. Użyj modułu `json` # 3. Wynik zapisz do pliku `FILE` # 4. Uruchom doctesty - wszystkie muszą się powieść # %% Hints # - `with open(mode='wt') as file:` # - `json.dump()` # %% 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' in globals(), \ 'Variable `result` is not defined; assign result of your program to it.' >>> 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.' >>> print(result) # doctest: +NORMALIZE_WHITESPACE [{"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': '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