What is JSON?
JSON (JavaScript Object Notation) is a language-independent text format for storing and exchanging data. Web applications use JSON to exchange data between the web browser and the server, and REST APIs are used to exchange data between servers. There are ready-made code libraries for creating and manipulating JSON data for many programming languages, including JavaScript, Java, C ++, C #, Go, PHP, and Python. JSON files are named with the .json extension.
How can I work with JSON in Python?
Python has a built-in json module (JSON encoder and decoder) to work with JSON. To work with this json module in Python, you need to import it first.
Converting Python Object to JSON Example
The following is an example of converting a Python object to human-readable JSON format:
How to convert a Python object to a human-readable JSON format?
When converting a Python object to JSON, you can pretty-print the JSON output into human-readable JSON format by passing an indentation level to the json.dumps() method.
How to convert an object with multiple data types to JSON in Python?
The following is an example of converting a Python object containing multiple data types to JSON in Python:
How does JSON Encoder convert Python object to JSON string?
Python has different types of data because JSON is an independent data storage format that comes from the JavaScript world. The JSON Encoder only supports the following basic types and data structures and will throw an exception for other Python object types:
Python | JSON |
---|---|
dict | object |
list, tuple | array |
str | string |
int, float | number |
True | true |
False | false |
None | null |