How to Convert Object to JSON in Python


In this example we will show how to convert from Python to JSON in Python.

Source Code

import json

# a Python object (dict)
my_dict = {
    'name': 'Jim',
    'age': 14,
    'id': '15'
}

# convert from Python to JSON
python_to_json = json.dumps(my_dict)

# the result is a JSON string
print(python_to_json)

Output:

{"name": "Jim", "age": 14, "id": "15"}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments