6.2. JSON To String

6.2.1. Mapping

  • json.dumps(DATA: dict) -> str

  • json.loads(DATA: str) -> dict

Serialize mapping to JSON:

>>> import json
>>>
>>>
>>> DATA = {'firstname': 'Mark',
...         'lastname': 'Watney'}
>>>
>>>
>>> result = json.dumps(DATA)
>>>
>>> print(result)
{"firstname": "Mark", "lastname": "Watney"}

Deserialize JSON to mapping:

>>> import json
>>>
>>>
>>> DATA = """
...     {"firstname": "Mark",
...      "lastname": "Watney"}"""
>>>
>>>
>>> result = json.loads(DATA)
>>>
>>> print(result)
{'firstname': 'Mark', 'lastname': 'Watney'}

6.2.2. List of Mappings

  • json.dumps(data: Sequence[dict]) -> str

  • json.loads(data: str) -> list[dict]

Serialize list of mappings to JSON:

>>> import 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:

>>> import json
>>>
>>>
>>> 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'}]

6.2.3. List of Sequences

  • json.dumps(data: list[Sequence]) -> str

  • json.loads(data: str) -> list[list]

Serialize list of sequences to JSON:

>>> 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')]
>>>
>>>
>>> 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 sequences:

>>> 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"]]"""
>>>
>>>
>>> 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']]

6.2.4. Assignments