Menu Close

How to Convert Python Dictionary to JSON

How to convert Python dictionary to json

Here we are going to learn how to convert Python dictionary to JSON with the help of Python. To convert the python dictionary to JSON we will use a built-in JSON module.

Before going through this article, we will know little about Python dictionaries and JSON (JavaScript Object Notation ).

What is Python Dictionary ?

Python dictionary is one of the important data types in Python that stores the data in the form of key and value. Python dictionary does not store duplicate data. Any dictionary value can be access with the help of their corresponding key.

Let’s understand the Python dictionary with the help of the example.


student = {
"id": "ABC",
"age": 21,
"subject": ["Math", "Python", "C++", "MySQL"],
"address": "India"
}

As you can see in the above example we have defined a Python dictionary named student which stores information about a single student. All the data is stored in the form of keys and values.

What is JSON (JavaScript Object Notation ) ?

JSON stands for ( JavaScript Object Notation ). JSON is one of the most popular formats for store the formatted data.JSON is basically used to transmit the data to the server. Suppose you want to send some data to the server or receive data from the server then your data should be in JSON format because JSON is an easy-to-understand data format.

You can see in the below example JSON data that converted from the above Python dictionary student.


{
  "id": "ABC",
  "age": 21,
  "subject": [
    "Math",
    "Python",
    "C++",
    "MySQL"
  ],
  "address": "India"
}

Let’s see how can we convert the python dictionary to JSON.

Python provides us a built-in module JSON which gives us some function to work with JSON data or Python dictionary.

Convert Python dictionary to JSON

There are two functions provided by the Python JSON module to work with dictionaries.

  • json.dumps()
  • json.dump()

json.dumps()

json.dumps() is the common function to convert Python dictionaries to JSON data. It is mostly used by the Python developers.json.dumps() accept python dictionary as a parameter. and return string as an output.

Example


import json

# python dictionary 

student = {
"id": "ABC",
"age": 21,
"subject": ["Math", "Python", "C++", "MySQL"],
"address": "India"
}

# convert Python dictionary to json
result = json.dumps(student)
print(result)

Output


{
  "id": "ABC",
  "age": 21,
  "subject": [
    "Math",
    "Python",
    "C++",
    "MySQL"
  ],
  "address": "India"
}

You can also sort the keys of the JSON data passing the sort_keys=True parameter to dumps() method.

Example


import json

# python dictionary 

student = {
"id": "ABC",
"age": 21,
"subject": ["Math", "Python", "C++", "MySQL"],
"address": "India"
}

# convert Python dictionary to json
result = json.dumps(student, sort_keys=True)
print(result)

Output


{
  "address": "India",
  "age": 21,
  "id": "ABC",
  "subject": [
    "Math",
    "Python",
    "C++",
    "MySQL"
  ]
}

json.dump()

json.dumps() is the common function to convert Python dictionaries to JSON data.Unlike json.dumps() because it accepts python dictionary and file pinter as a parameter and returns a string as an output.file pointer should be open in write and append mode. json.dump() the function is basically used to write JSON data into a file.

Example


import json

# python dictionary 

student = {
"id": "ABC",
"age": 21,
"subject": ["Math", "Python", "C++", "MySQL"],
"address": "India"
}

# convert Python dictionary to json

with open("data.json", "w") as file_pointer:
	result = json.dump(student, file_ponter)

In the above example, we have opened a file named data.json to write the JSON data. If you execute the above Python script it will create a file named data.json file ( If not exist ), Then convert all the Python dictionary to JSON and write to data.json file.

data.json


{
  "id": "ABC",
  "age": 21,
  "subject": [
    "Math",
    "Python",
    "C++",
    "MySQL"
  ],
  "address": "India"
}

Summary

In this article, we have seen all about the process to convert Python dictionary to JSON ( JavaScript Object Notation ) data with the help of Python pre-defined JSON module.
Python JSON module is going to be very helpful when you’re creating the rest of API using Python because using this you can convert your Python dictionary to JSON data to be returned as a response.
I hope this article will help you. If you like this article, please share and keep visiting for further Python interesting tutorials.

Reference:- Click Here

Thanks for reading…

Previous Article
Next Article

Was this post helpful?

Related Posts