How to Define Numbers of Indents in Python


In this example, we will learn how to define the numbers of indents using the indent parameter in the json.dumps() method in Python.

Source Code

import json

my_dict = {
    'name': 'Jim',
    'age': 14,
    'id': '15'
}

python_to_json_1 = json.dumps(my_dict)
print(python_to_json_1)

python_to_json_2 = json.dumps(my_dict, indent=4)
print(python_to_json_2)

Output:

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