Convert Python Dictionary to Json

Jan 20, 2023

3 mins read

Published in

Using the JSON module, you can convert a Python dictionary to a JSON object. The json.dumps() function converts a Python dictionary to a JSON string, while json.dump() writes the JSON string to a file-like object.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import json

# create a Variable of Python dictionary
data = {'name': 'Tom Cruise', 'age': 60, 'city': 'New York'}

# convert dict to JSON
json_data = json.dumps(data)

# print the Converted JSON object
print(json_data)

What is Python Dictionary?

A dictionary in Python is a group of key-value pairs. It is similar to a list or an array in other programming languages but has keys instead of indexes. Each key is unique and is associated with a value. The item in a dictionary object can be of any data type, such as strings, integers, or other objects.

A dictionary is defined using curly braces {} and is enclosed in square brackets []. Each key-value pair is separated by a colon : , and different pairs are separated by a comma , .

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# create an empty dictionary
my_dict = {}

# create a dictionary with key-value pairs
my_dict = {'name': 'Tom Cruise', 'age': 60, 'city': 'New York'}

# access the value of a key
print(my_dict['name']) # Output: John Smith

# add a new key-value pair
my_dict['gender'] = 'male'

# update the value of a key
my_dict['age'] = 36

# delete a key-value pair
del my_dict['city']

You can use the keys() and values() functions to get the keys and values from a dictionary, respectively.

The items() function returns a list of key-value pairs in the form of a tuple.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
my_dict = {'name': 'Tom Cruise', 'age': 60, 'city': 'New York'}

# get the keys
keys = my_dict.keys()

# get the values
values = my_dict.values()

#get the items
items = my_dict.items()

Dictionaries are helpful when you need to store and retrieve data based on keys rather than indices, and they are widely used in Python for various data manipulation tasks.

Alternatively, you can use the json.dump() function to write the JSON data to a file:

1
2
3
4
5
6
import json

data = {'name': 'Tom Cruise', 'age': 60, 'city': 'New York'}

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

Save Dictionary as json python

Users can use the JSON module of Python to save a dictionary as a JSON file. The json.dump() function writes the contents of a Python dictionary to a file-like object in JSON format.

1
2
3
4
5
6
7
8
9
import json

# create a Python dictionary
data = {'name': 'Tom Cruise', 'age': 60, 'city': 'New York'}

# open a file for writing
with open('data.json', 'w') as f:
    # write the dictionary to the file in JSON format
    json.dump(data, f)

Convert dict to JSON Python using json.dumps()

Alternatively, you can use the json.dumps() function to convert the dictionary to a JSON string and then write the string to a file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import json

data = {'name': 'Tom Cruise', 'age': 60, 'city': 'New York'}

# convert dictionary to JSON string
json_data = json.dumps(data)

# write the JSON string to a file
with open('data.json', 'w') as f:
    f.write(json_data)

In either case, the resulting JSON file, ‘data.json’ will contain the data from the dictionary in the specified format.

You can also use the json.dump() function to write the dictionary to a file and json.dumps() to convert it to a JSON string and print it.

Know more about Python Dictionary to Json

Sharing is caring!