How To Convert A Dictionary To JSON in Python – With Examples

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.

This tutorial teaches you how to convert a dictionary to JSON in python and how to use the indent and the sort_keys parameter.

If You’re in Hurry…

You can use the json.dumps() method to convert a dictionary to JSON. The JSON library is available by default.

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'>

If You Want to Understand Details, Read on…

There are different methods and settings while converting a dictionary to JSON in Python. Let us learn each method.

Using dumps() function

This section teaches you how to use the json.dumps() method.

The dumps() method converts the passed dictionary object to a JSON formatted String.

It accepts a dictionary as a parameter and returns a JSON-formatted string.

Code

The following code demonstrates how to use the dumps() method to convert a dictionary into a JSON String.

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.

You can 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

The following code uses the indent parameter with the dumps() method.

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

This section teaches you how to sort keys while converting the dictionary into a JSON string.

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, you can use the sort_keys=True parameter.

Code

The following code demonstrates using the sort_key parameter while converting a dictionary into a JSON string.

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.

You can 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.

You can 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"}

Conclusion

You’ve learned how to convert a dictionary into JSON in Python. Additionally, you’ve learned how to use the indent parameter to format the string and how to use the sort_keys parameter to sort the keys before converting to JSON.

If you have any questions, please feel free to comment below.

You May Also Like

Leave a Comment