Dictionary allows you to store value in a key-value format, and JSON is a lightweight data-interchange format.
You can convert a dictionary to JSON in python using the json.dumps(yourdict) method.
Basic Example
import json
yourdict = {'Name': 'Ram', 'Job': 'Expert', 'Salary': '90000'}
result = json.dumps(yourdict)
print(result)
print(type(result))
Output
{"Name": "Ram", "Job": "Expert", "Salary": "90000"}
<class 'str'>
This tutorial teaches you how to convert a dictionary to JSON in python and how to use the indent
and the sort_keys
parameter.
Using dumps() function
The dumps()
method converts the passed dictionary object to a JSON formatted String.
- It accepts a dictionary as a parameter
- Returns a JSON-formatted string.
Code
import json
yourdict = {'Name': 'Ram', 'Job': 'Expert', 'Salary': '90000'}
result = json.dumps(yourdict)
print(result)
print(type(result))
Output
{"Name": "Ram", "Job": "Expert", "Salary": "90000"}
<class 'str'>
Using dumps() With Indent
It is easier to read a String when it is properly indented and formatted.
- Use the
indent
parameter to convert the dictionary with each key-value pair in a newline and proper indentation.
Writing the content of a dictionary with an indent is also known as pretty printing.
Code
import json
yourdict = {'Name': 'Ram', 'Job': 'Expert', 'Salary': '90000'}
result = json.dumps(yourdict, indent = 3)
result
Output
{
"Name": "Ram",
"Job": "Expert",
"Salary": "90000"
}
Using dumps() With sort_keys
Since Python 3.7, the insertion order is maintained in the dictionary. Hence, if the keys are inserted in a sorted manner, they’ll also be retrieved in the same order.
However, the older version of Python doesn’t maintain the key insertion orders.
To sort the keys while converting the dictionary into a JSON string,
- Use the
sort_keys=True
parameter.
Code
import json
yourdict = {'Name': 'Ram', 'Job': 'Expert', 'Salary': '90000'}
result = json.dumps(yourdict, sort_keys = True)
print(result)
Output
{"Job": "Expert", "Name": "Ram", "Salary": "90000"}
Convert Nested Dictionary To JSON
This section demonstrates how to convert a nested dictionary into a JSON string.
- Use the same
json.dumps()
method and pass the nested dictionary as a parameter. - Using the indent parameter while using the nested dictionary is recommended so that the output string will be formatted and easily readable.
Code
import json
yourdict ={
"Ram": {'Job': 'Expert', 'Salary': '90000'},
"Michael" : {'Job': 'Developer', 'Salary': '80000'},
}
json_object = json.dumps(yourdict, indent = 3, sort_keys = True)
print(json_object)
Output
{
"Michael": {
"Job": "Developer",
"Salary": "80000"
},
"Ram": {
"Job": "Expert",
"Salary": "90000"
}
}
Convert Dictionary to JSON File
This section teaches you how to convert a dictionary into a JSON string and write it into a file.
- Use the
dump()
method and pass the dictionary and file object. - The dictionary will be converted into JSON string and written into a file.
To learn in detail about writing a dictionary into a JSON file, read: – How To Dump A Dictionary Into A JSON File in Python
Code
import json
yourdict = {'Name': 'Ram', 'Job': 'Expert', 'Salary': '90000'}
with open('result.json', 'w') as fp:
json.dump(yourdict, fp)
Output
You’ll see the following content when you open the result.json
file.
{"Name": "Ram", "Job": "Expert", "Salary": "90000"}