0
0
PythonProgramBeginner · 2 min read

Python Program to Convert Dictionary to JSON

Use the json.dumps() function from Python's json module to convert a dictionary to a JSON string, like json.dumps(your_dict).
📋

Examples

Input{"name": "Alice", "age": 30}
Output{"name": "Alice", "age": 30}
Input{"city": "New York", "temperature": 22, "is_sunny": true}
Output{"city": "New York", "temperature": 22, "is_sunny": true}
Input{}
Output{}
🧠

How to Think About It

To convert a dictionary to JSON, think of turning a Python data box into a string that looks like JSON. We use the json.dumps() function which takes the dictionary and returns a JSON formatted string. This string can then be saved or sent anywhere JSON is needed.
📐

Algorithm

1
Import the json module.
2
Create or get the dictionary you want to convert.
3
Use json.dumps() with the dictionary as input.
4
Store or print the resulting JSON string.
💻

Code

python
import json

my_dict = {"name": "Alice", "age": 30}
json_str = json.dumps(my_dict)
print(json_str)
Output
{"name": "Alice", "age": 30}
🔍

Dry Run

Let's trace converting {'name': 'Alice', 'age': 30} to JSON string.

1

Import json module

The program loads the json module to access JSON functions.

2

Create dictionary

my_dict = {'name': 'Alice', 'age': 30}

3

Convert dict to JSON string

json_str = json.dumps(my_dict) results in '{"name": "Alice", "age": 30}'

4

Print JSON string

Output is printed: {"name": "Alice", "age": 30}

StepActionValue
1Import json modulejson module ready
2Create dictionary{'name': 'Alice', 'age': 30}
3Convert dict to JSON string{"name": "Alice", "age": 30}
4Print JSON stringPrinted output
💡

Why This Works

Step 1: Import json module

The json module provides tools to work with JSON data in Python.

Step 2: Use json.dumps()

json.dumps() converts a Python dictionary into a JSON formatted string.

Step 3: Print the JSON string

Printing shows the JSON string which can be used for data exchange or storage.

🔄

Alternative Approaches

Using json.dump() to write JSON directly to a file
python
import json
my_dict = {"name": "Alice", "age": 30}
with open('data.json', 'w') as f:
    json.dump(my_dict, f)
print('JSON saved to file')
This method writes JSON directly to a file instead of returning a string.
Pretty printing JSON with indentation
python
import json
my_dict = {"name": "Alice", "age": 30}
json_str = json.dumps(my_dict, indent=4)
print(json_str)
Adds indentation to make JSON easier to read for humans.

Complexity: O(n) time, O(n) space

Time Complexity

The time depends on the number of items in the dictionary because json.dumps() processes each key-value pair once.

Space Complexity

The space used grows with the size of the dictionary since the JSON string stores all data.

Which Approach is Fastest?

Using json.dumps() is fast and simple for in-memory conversion; writing directly to a file with json.dump() avoids storing the whole string in memory.

ApproachTimeSpaceBest For
json.dumps()O(n)O(n)Converting dict to JSON string in memory
json.dump() to fileO(n)O(1)Saving JSON directly to a file without extra memory
json.dumps() with indentO(n)O(n)Readable JSON output for humans
💡
Always import the json module before converting dictionaries to JSON strings.
⚠️
Forgetting to import the json module causes a NameError when calling json.dumps().