The json library can parse JSON from strings or files. This library parses JSON and converts it to a Python dictionary or list. It can also convert Python dictionaries or lists to JSON strings.
parsing JSON
Create the following string containing the JSON data
1 |
|
It can be parsed like this:
1 2 |
|
It can then be used like a regular dictionary:
1 2 |
|
You can convert the following object to JSON:
1 2 3 4 5 6 7 |
|
simplejson
The JSON library was added in version 2.6 of Python. If you use an earlier version of Python, the simplejson library is available via PyPI.
simplejson is similar to the json standard library, which enables developers using older versions of Python to use the latest features in the json library.
If the json library is not available, you can alias simplejson to json:
1 |
|
After importing simplejson as json, the above example will work as if you were using the standard json library.