JSON (JavaScript Object Notation) is often used on the web to exchange data.
JSON can be encoded and decoded with Python. That means you can convert from Python objects to JSON objects and vice-versa.
Related course: Python Programming Courses & Exercises
Python json dumps
In the example below we convert a Python object to a JSON object.
Start with a class (Student), create the object (pythonObj) and
finally convert it to JSON using the method json.dumps().
import json |
This outputs:
abstract-base-classes.md
{"password": "123456", "id": 1, "name": "ashley"} |
Python json loads
If you want to convert JSON data to a Python dictionary, just use loads:
import json |
To convert it to a Python object, define the class and pass the parameters as a map.
import json |
