7.2. JSON String¶
7.2.1. SetUp¶
>>> import json
7.2.2. Mapping¶
json.dumps(DATA: dict) -> str
- dict to JSONjson.loads(DATA: str) -> dict
- JSON to dict
Serialize mapping to JSON:
>>> DATA = {
... 'firstname': 'Mark',
... 'lastname': 'Watney',
... }
>>>
>>> result = json.dumps(DATA)
>>> print(result)
{"firstname": "Mark", "lastname": "Watney"}
Deserialize JSON to mapping:
>>> DATA = """{
... "firstname": "Mark",
... "lastname": "Watney"
... }"""
>>>
>>> result = json.loads(DATA)
>>> print(result)
{'firstname': 'Mark', 'lastname': 'Watney'}
7.2.3. List of Mappings to JSON¶
json.dumps(data: Iterable[dict]) -> str
- list[dict] to JSONjson.loads(data: str) -> list[dict]
- JSON to list[dict]
Serialize list of mappings to JSON:
>>> DATA = [
... {'firstname': 'Melissa', 'lastname': 'Lewis'},
... {'firstname': 'Rick', 'lastname': 'Martinez'},
... {'firstname': 'Mark', 'lastname': 'Watney'},
... ]
>>>
>>> result = json.dumps(DATA)
>>> print(result)
[{"firstname": "Melissa", "lastname": "Lewis"},
{"firstname": "Rick", "lastname": "Martinez"},
{"firstname": "Mark", "lastname": "Watney"}]
Deserialize JSON to list of mappings:
>>> DATA = """[
... {"firstname": "Melissa", "lastname": "Lewis"},
... {"firstname": "Rick", "lastname": "Martinez"},
... {"firstname": "Mark", "lastname": "Watney"}
... ]"""
>>>
>>> result = json.loads(DATA)
>>> print(result)
[{'firstname': 'Melissa', 'lastname': 'Lewis'},
{'firstname': 'Rick', 'lastname': 'Martinez'},
{'firstname': 'Mark', 'lastname': 'Watney'}]
7.2.4. List of Iterable¶
json.dumps(data: list[Iterable]) -> str
- list[Iterable] to JSONjson.loads(data: str) -> list[list]
- JSON to list[Iterable]
Serialize list of iterables to JSON:
>>> DATA = [
... ('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'),
... (5.8, 2.7, 5.1, 1.9, 'virginica'),
... (5.1, 3.5, 1.4, 0.2, 'setosa'),
... (5.7, 2.8, 4.1, 1.3, 'versicolor')
... ]
>>>
>>> result = json.dumps(DATA)
>>> print(result)
[["sepal_length", "sepal_width", "petal_length", "petal_width", "species"],
[5.8, 2.7, 5.1, 1.9, "virginica"],
[5.1, 3.5, 1.4, 0.2, "setosa"],
[5.7, 2.8, 4.1, 1.3, "versicolor"]]
Deserialize JSON to list of iterables:
>>> DATA = """[
... ["sepal_length", "sepal_width", "petal_length", "petal_width", "species"],
... [5.8, 2.7, 5.1, 1.9, "virginica"],
... [5.1, 3.5, 1.4, 0.2, "setosa"],
... [5.7, 2.8, 4.1, 1.3, "versicolor"]
... ]"""
>>>
>>> result = json.loads(DATA)
>>> print(result)
[['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'],
[5.8, 2.7, 5.1, 1.9, 'virginica'],
[5.1, 3.5, 1.4, 0.2, 'setosa'],
[5.7, 2.8, 4.1, 1.3, 'versicolor']]
7.2.5. Assignments¶
"""
* Assignment: JSON String DumpFlat
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min
English:
1. Dump `DATA` to JSON format
2. Run doctests - all must succeed
Polish:
1. Zrzuć `DATA` do formatu JSON
2. Uruchom doctesty - wszystkie muszą się powieść
Hints:
* json.dumps()
Tests:
>>> import sys; sys.tracebacklimit = 0
>>> 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'
>>> print(result)
[5.1, 3.5, 1.4, 0.2, "setosa"]
"""
import json
DATA = (5.1, 3.5, 1.4, 0.2, 'setosa')
# dump DATA to JSON format
# type: str
result = ...
"""
* Assignment: JSON String DumpNested
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min
English:
1. Dump `DATA` to JSON format
2. Run doctests - all must succeed
Polish:
1. Zrzuć `DATA` do formatu JSON
2. Uruchom doctesty - wszystkie muszą się powieść
Hints:
* json.dumps()
Tests:
>>> import sys; sys.tracebacklimit = 0
>>> 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'
>>> print(result) # doctest: +NORMALIZE_WHITESPACE
[["sepal_length", "sepal_width", "petal_length", "petal_width", "species"],
[5.8, 2.7, 5.1, 1.9, "virginica"],
[5.1, 3.5, 1.4, 0.2, "setosa"],
[5.7, 2.8, 4.1, 1.3, "versicolor"],
[6.3, 2.9, 5.6, 1.8, "virginica"],
[6.4, 3.2, 4.5, 1.5, "versicolor"],
[4.7, 3.2, 1.3, 0.2, "setosa"]]
"""
import json
DATA = [
('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'),
(5.8, 2.7, 5.1, 1.9, 'virginica'),
(5.1, 3.5, 1.4, 0.2, 'setosa'),
(5.7, 2.8, 4.1, 1.3, 'versicolor'),
(6.3, 2.9, 5.6, 1.8, 'virginica'),
(6.4, 3.2, 4.5, 1.5, 'versicolor'),
(4.7, 3.2, 1.3, 0.2, 'setosa'),
]
# dump DATA to JSON format
# type: str
result = ...
"""
* Assignment: JSON String LoadObject
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min
English:
1. Load `DATA` from JSON format
2. Convert data to `result: dict`
3. Run doctests - all must succeed
Polish:
1. Wczytaj `DATA` z formatu JSON
2. Przekonwertuj dane do `result: dict`
3. Uruchom doctesty - wszystkie muszą się powieść
Hints:
* json.loads()
Tests:
>>> import sys; sys.tracebacklimit = 0
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is dict, \
'Variable `result` has invalid type, should be dict'
>>> assert len(result) > 0, \
'Variable `result` should not be empty'
>>> result # doctest: +NORMALIZE_WHITESPACE
{'sepal_length': 5.1,
'sepal_width': 3.5,
'petal_length': 1.4,
'petal_width': 0.2,
'species': 'setosa'}
"""
import json
DATA = """
{
"sepal_length": 5.1,
"sepal_width": 3.5,
"petal_length": 1.4,
"petal_width": 0.2,
"species": "setosa"
}
"""
# Load `DATA` from JSON format
# type: list[dict]
result = ...
"""
* Assignment: JSON String LoadList
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min
English:
1. Load `DATA` from JSON format
2. Convert data to `result: list[dict]`
3. Run doctests - all must succeed
Polish:
1. Wczytaj `DATA` z formatu JSON
2. Przekonwertuj dane do `result: list[dict]`
3. Uruchom doctesty - wszystkie muszą się powieść
Hints:
* json.loads()
Tests:
>>> import sys; sys.tracebacklimit = 0
>>> 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 dict for row in result), \
'Variable `result` should be a list[dict]'
>>> result[0] # doctest: +NORMALIZE_WHITESPACE
{'sepal_length': 5.8,
'sepal_width': 2.7,
'petal_length': 5.1,
'petal_width': 1.9,
'species': 'virginica'}
>>> result # doctest: +NORMALIZE_WHITESPACE
[{'sepal_length': 5.8, 'sepal_width': 2.7, 'petal_length': 5.1, 'petal_width': 1.9, 'species': 'virginica'},
{'sepal_length': 5.1, 'sepal_width': 3.5, 'petal_length': 1.4, 'petal_width': 0.2, 'species': 'setosa'},
{'sepal_length': 5.7, 'sepal_width': 2.8, 'petal_length': 4.1, 'petal_width': 1.3, 'species': 'versicolor'},
{'sepal_length': 6.3, 'sepal_width': 2.9, 'petal_length': 5.6, 'petal_width': 1.8, 'species': 'virginica'},
{'sepal_length': 6.4, 'sepal_width': 3.2, 'petal_length': 4.5, 'petal_width': 1.5, 'species': 'versicolor'},
{'sepal_length': 4.7, 'sepal_width': 3.2, 'petal_length': 1.3, 'petal_width': 0.2, 'species': 'setosa'},
{'sepal_length': 7.0, 'sepal_width': 3.2, 'petal_length': 4.7, 'petal_width': 1.4, 'species': 'versicolor'},
{'sepal_length': 7.6, 'sepal_width': 3.0, 'petal_length': 6.6, 'petal_width': 2.1, 'species': 'virginica'},
{'sepal_length': 4.9, 'sepal_width': 3.0, 'petal_length': 1.4, 'petal_width': 0.2, 'species': 'setosa'}]
"""
import json
DATA = (
'[{"sepal_length":5.8,"sepal_width":2.7,"petal_length":5.1,"petal_widt'
'h":1.9,"species":"virginica"},{"sepal_length":5.1,"sepal_width":3.5,"'
'petal_length":1.4,"petal_width":0.2,"species":"setosa"},{"sepal_lengt'
'h":5.7,"sepal_width":2.8,"petal_length":4.1,"petal_width":1.3,"specie'
's":"versicolor"},{"sepal_length":6.3,"sepal_width":2.9,"petal_length"'
':5.6,"petal_width":1.8,"species":"virginica"},{"sepal_length":6.4,"se'
'pal_width":3.2,"petal_length":4.5,"petal_width":1.5,"species":"versic'
'olor"},{"sepal_length":4.7,"sepal_width":3.2,"petal_length":1.3,"peta'
'l_width":0.2,"species":"setosa"},{"sepal_length":7.0,"sepal_width":3.'
'2,"petal_length":4.7,"petal_width":1.4,"species":"versicolor"},{"sepa'
'l_length":7.6,"sepal_width":3.0,"petal_length":6.6,"petal_width":2.1,'
'"species":"virginica"},{"sepal_length":4.9,"sepal_width":3.0,"petal_l'
'ength":1.4,"petal_width":0.2,"species":"setosa"}]'
)
# Load `DATA` from JSON format
# type: list[dict]
result = ...