Python program to convert a dictionary to JSON

How to convert a dictionary to JSON in python:

In this post, we will learn how to convert a dictionary to JSON in python. JSON or JavaScript Object Notation is a popular format that is used widely for interchange and store data.

It has many advantages like it is lightweight, it is easy for human and programming languages to read etc.

A JSON object can have key-value pairs and an array of values.

For example, the below is a JSON object:

{"name" : "Alex", "age" : 10}

For this object, two keys are there name and age and values for name is Alex and for age is 10.

What is dictionary in Python:

Dictionaries are use to store key-value pairs in Python. We can create a dictionary by putting all key-value pairs within a curly braces. For example:

givenDict = {"name": "Alex", "age": 19}

givenDict is a dictionary.

We can’t have any duplicate keys in python.

Converting dictionary to JSON in python:

We can convert a dictionary to a JSON object easily in python. Python provides a module called json that provides different methods to work with JSON values.

It provides a method called dumps, that can be used to convert a dictionary to JSON. This method returns the converted JSON value.

Python example program to convert dictionary to JSON:

Let’s take a look at the below program:

import json

givenDict = {"name": "Alex", "age": 19}

jsonDict = json.dumps(givenDict)

print(jsonDict)

For this example,

  • We are using import json to import the json module. dumps method is defined in this module.
  • givenDict is the given dictionary.
  • We are using json.dumps to convert the dictionary to a JSON and that value is stored in jsonDict.
  • The last line is printing the JSON data.

If you run this, it will print the below output:

{"name": "Alex", "age": 19}

Python dictionary to JSON

Pretty print a JSON:

We can pass one more parameter index to json.dumps with a non-negative value. This is an integer value and it defines the indent level to use for pretty-print the JSON.

For example,

import json

givenDict = {"name": "Alex", "age": 19, "grade": "A"}

jsonDict = json.dumps(givenDict, indent=4)

print(jsonDict)

It will print:

{
    "name": "Alex",
    "age": 19,
    "grade": "A"
}

indent with None will make the most compact JSON.

And, if you pass 0, it will only insert newlines as like below:

{
"name": "Alex",
"age": 19,
"grade": "A"
}

Sorting the keys of a JSON:

We can also sort the keys of a JSON by using dumps. For that, we have to pass another parameter called sort_keys. This is False by default. If we pass True, it will sort the keys of the JSON.

For example,

import json

givenDict = {"name": "Alex", "age": 19, "grade": "A"}

jsonDict = json.dumps(givenDict, indent=4, sort_keys=True)

print(jsonDict)

It will sort the keys of the final JSON and print the data as like below:

{
    "age": 19,
    "grade": "A",
    "name": "Alex"
}

Writing Dictionary to a JSON file:

There is another method called dump defined in the json module. This method can be used to write the content of a dictionary to a file. It can be a JSON file or any other file.

This method takes two parameters: the first one is the dictionary that we need to convert and write and the second one is the pointer to the file.

For example:

import json

givenDict = {"name": "Alex", "age": 19, "grade": "A"}

with open("data.json", "w") as f:
    json.dump(givenDict, f)

It will print the content of givenDict to a file data.json.

You can also change the indent and sort the keys similar to dumps.

import json

givenDict = {"name": "Alex", "age": 19, "grade": "A"}

with open("data.json", "w") as f:
    json.dump(givenDict, f, indent=4, sort_keys=True)

You might also like: