Converting Python Object to JSON

To convert a Python object to a JSON string, you can use the json.dumps() method of the built-in json module. The json.dumps() method converts Python objects such as dictionaries, lists, tuples, and strings to their equivalent JSON objects. IIf your Python object contains objects of a non-base type, the json.dumps() method will throw an exception. To avoid the exception, you can pass the skipkeys=True parameter to json.dumps(). This will tell Python to ignore these objects when converting to JSON. If you pass the "indent" parameter to the json.dumps() method, you can print the JSON result nicely; otherwise, compact JSON will be generated. In this Python Convert Object to JSON example, we serialize a basic Python object into a JSON string. Click Execute to run the Python Object to JSON example online and see the result.
Converting Python Object to JSON Execute
import json

obj = {
  "Rating": 1,
  "User": "Max Sher",
}

json_str = json.dumps(obj, indent=2)
      
print(json_str)
Updated: Viewed: 4837 times

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.

JSON Example
{
  "Id": 354,
  "Customer": "Max Sher",
  "Status": "VIP"
}

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.

Python Object to JSON Example
import json

json_str = json.dumps({"Customer": "Max Sher"})
        
print(json_str)

# output: {"Customer": "Max Sher"}

Converting Python Object to JSON Example

The following is an example of converting a Python object to human-readable JSON format:

Convert Python Object to JSON Example
import json

obj = {
  "Id": 12,
  "Employee": "Leo",
}

json_str = json.dumps(obj)
      
print(json_str)

# {"Id": 12, "Employee": "Leo"}

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.

Convert Python Object to Pretty JSON Format Example
import json

json_str = json.dumps({"User": "Max Sher", "Status": "VIP" }, indent=2)

print(json_str)

# output:
# {
#   "User": "Max Sher",
#   "Status": "VIP"
# }

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:

Convert Python Object with Multiple Data Types to JSON Example
import json

obj = {
  "name" : "Max Sher",
  "id" : 354,
  "work" : False,
  "graphic" : None,
  "list1" : ["Max Sher", 354],
  "touple1" : ("Max Sher", True)
}

json_str = json.dumps(obj, indent=2)
      
print(json_str)

# output: 
# {
#   "name": "Max Sher",
#   "id": 354,
#   "work": false,
#   "graphic": null,
#   "list1": [
#     "Max Sher",
#     354
#   ],
#   "touple1": [
#     "Max Sher",
#     true
#   ]
# }

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

See also