0
0
PythonHow-ToBeginner · 2 min read

Python How to Convert Dictionary to JSON String

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"}
Output"{\"name\": \"Alice\"}"
Input{"age": 30, "city": "New York"}
Output"{\"age\": 30, \"city\": \"New York\"}"
Input{}
Output"{}"
🧠

How to Think About It

To convert a dictionary to a JSON string, think of turning the dictionary into a text format that looks like JSON. Python's json module has a function called dumps() that does this conversion easily by taking the dictionary and returning a string with JSON formatting.
📐

Algorithm

1
Import the json module.
2
Prepare or get the dictionary you want to convert.
3
Call the json.dumps() function with the dictionary as argument.
4
Store or print the returned JSON string.
💻

Code

python
import json

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

Dry Run

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

1

Import json module

json module is ready to use.

2

Create dictionary

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

3

Convert dictionary to JSON string

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

StepDictionaryJSON String
Initial{'name': 'Alice', 'age': 30}N/A
After dumps(){'name': 'Alice', 'age': 30}{"name": "Alice", "age": 30}
💡

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() takes a Python dictionary and converts it into a JSON formatted string.

Step 3: Print or use the JSON string

The result is a string that can be saved, sent, or used where JSON format is needed.

🔄

Alternative Approaches

Using json.dumps() with indent for pretty print
python
import json
my_dict = {"name": "Alice", "age": 30}
json_string = json.dumps(my_dict, indent=4)
print(json_string)
Adds indentation and line breaks to make the JSON string easier to read.
Using json.dumps() with sort_keys
python
import json
my_dict = {"b": 2, "a": 1}
json_string = json.dumps(my_dict, sort_keys=True)
print(json_string)
Sorts the dictionary keys alphabetically in the JSON string.

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 is proportional to the size of the dictionary since the output is a string representation of all items.

Which Approach is Fastest?

Using json.dumps() directly is the fastest and simplest way; adding options like indent or sort_keys adds minor overhead.

ApproachTimeSpaceBest For
json.dumps()O(n)O(n)Simple conversion
json.dumps() with indentO(n)O(n)Readable JSON output
json.dumps() with sort_keysO(n log n)O(n)Sorted keys in JSON
💡
Always import the json module before using json.dumps() to convert dictionaries.
⚠️
Trying to convert a dictionary to JSON string without importing the json module first.