Convert Python Dict to JSON

In the below code, we are converting a Python dictionary to a JSON object using json.dumps() method of JSON module in Python. We first import the JSON module and then make a small dictionary with some key-value pairs and then passed it into json.dumps() method with ‘indent=4’ to convert this Python dictionary into a JSON object. As we have given the value of indent to 4 there are four whitespaces before each data as seen in the output.

Python3




# Python program to convert
# Python to JSON
import json
  
# Data to be written
dictionary = {
  "id": "04",
  "name": "sunil",
  "department": "HR"
}
  
# Serializing json
json_object = json.dumps(dictionary, indent = 4)
print(json_object)


Output

{
    "id": "04",
    "name": "sunil",
    "department": "HR"
}

The following types of Python objects can be converted into JSON strings: 

Python objects and their equivalent conversion to JSON:

Python

JSON Equivalent

dict

object

list, tuple

array

str

string

int, float

number

True

true

False

false

None

null

Read, Write and Parse JSON using Python

JSON is a lightweight data format for data interchange that can be easily read and written by humans, and easily parsed and generated by machines. It is a complete language-independent text format. To work with JSON data, Python has a built-in package called JSON.

Example of JSON String

s = '{"id":01, "name": "Emily", "language": ["C++", "Python"]}'

The syntax of JSON is considered a subset of the syntax of JavaScript including the following:

  • Name/Value pairs: Represents Data, the name is followed by a colon(:), and the Name/Value pairs are separated by a comma(,).
  • Curly braces: Holds objects.
  • Square brackets: Hold arrays with values separated by a comma (,).

Keys/Name must be strings with double quotes and values must be data types amongst the following: 

Example of JSON file:

 {
"employee": [
{
"id": "01",
"name": "Amit",
"department": "Sales"
},
{
"id": "04",
"name": "sunil",
"department": "HR"
}
]
}

Similar Reads

Python Parse JSON String

In the below code, we are going to convert JSON to a Python object. To parse JSON string Python firstly we import the JSON module. We have a JSON string stored in a variable ’employee’ and we convert this JSON string to a Python object using json.loads() method of JSON module in Python. After that, we print the name of an employee using the key ‘name’ ....

Python read JSON file

...

Convert Python Dict to JSON

Let’s suppose we have a JSON file that looks like this....

Writing JSON to a file in Python

...

Python Pretty Print JSON

In the below code, we are converting a Python dictionary to a JSON object using json.dumps() method of JSON module in Python. We first import the JSON module and then make a small dictionary with some key-value pairs and then passed it into json.dumps() method with ‘indent=4’ to convert this Python dictionary into a JSON object. As we have given the value of indent to 4 there are four whitespaces before each data as seen in the output....