Table of Contents
JSON - A Popular Data Format for Presenting Structured Data
JSON is a popular data format for presenting structured data. It’s a prevalent practice to transmit and receive data between the server and web application in JSON format.
JSON has several advantages over other formats, including being easy to read by humans, that it can be used with any programming language, that it doesn’t require you to know what types are in your object, and more! In this article we will go through how to parse JSON using Python.
How to Convert Python Dict to JSON?
Python Dictionary is an unordered collection of key-value pairs. JSON (JavaScript Object Notation) is a lightweight data interchange format based on JavaScript object notation. Python dict to json conversion can be done using the following steps:
Step One: Convert Dictionary to String
As you can see from above using json.dumps() method, for a dictionary we need to pass string as the first argument and it returns a JSON formatted value.
Step Two: Use json.loads() Method
Example 01: Code
import json
dictionary ={
"roll_no": "F16SW-75",
"name": "Anmol",
"department": "Software"
}
json_object = json.dumps(dictionary, indent = 4)
print("Data:\n", json_object)
Output

Example 02: Code
import json
dictionary ={
"name" : "Anmol",
"rollno" : 'F16SW-75',
"cgpa" : 3.55,
"ph_no" : "03335556668"
}
data = json.dumps(dictionary, sort_keys=True)
print("Data:\n",data)
Output

Conclusion
With this knowledge, you can convert your Python dict object to an equivalent JSON string representation with the json.dumps() and json.loads(). You can also use some of these other functions in order to manipulate your data into new formats that are more readable for humans or machines before converting it back to a dict object using python’s eval function.