0
0
PythonHow-ToBeginner · 2 min read

Python How to Convert List to JSON String

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

Examples

Input[1, 2, 3]
Output"[1, 2, 3]"
Input["apple", "banana", "cherry"]
Output"[\"apple\", \"banana\", \"cherry\"]"
Input[]
Output"[]"
🧠

How to Think About It

To convert a list to JSON, think of JSON as a text format that represents data structures. You take your Python list and turn it into a string that looks like JSON using the json.dumps() function. This string can then be saved or sent to other programs.
📐

Algorithm

1
Import the json module.
2
Prepare the list you want to convert.
3
Use json.dumps() with the list as argument.
4
Store or print the resulting JSON string.
💻

Code

python
import json

my_list = ["apple", "banana", "cherry"]
json_string = json.dumps(my_list)
print(json_string)
Output
["apple", "banana", "cherry"]
🔍

Dry Run

Let's trace converting ['apple', 'banana', 'cherry'] to JSON string.

1

Import json module

json module is ready to use.

2

Define list

my_list = ['apple', 'banana', 'cherry']

3

Convert list to JSON string

json_string = json.dumps(my_list) results in '["apple", "banana", "cherry"]'

StepActionValue
1Import jsonjson module ready
2Define list['apple', 'banana', 'cherry']
3Convert to JSON["apple", "banana", "cherry"]
💡

Why This Works

Step 1: Import json module

The json module provides tools to convert Python objects to JSON format.

Step 2: Use json.dumps()

json.dumps() takes a Python list and returns a string that represents the list in JSON format.

Step 3: Result is a JSON string

The output is a string that looks like a list but is formatted as JSON, which can be saved or sent over networks.

🔄

Alternative Approaches

Using json.dump() to write directly to a file
python
import json
my_list = [1, 2, 3]
with open('data.json', 'w') as f:
    json.dump(my_list, f)
This writes the JSON string directly to a file instead of returning it as a string.
Using str() for simple lists (not recommended)
python
my_list = [1, 2, 3]
json_string = str(my_list)
print(json_string)
This creates a string but not valid JSON (uses single quotes), so it is not suitable for JSON data exchange.

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

Time Complexity

The time to convert grows linearly with the number of elements in the list because each element must be processed.

Space Complexity

The output string size grows linearly with the list size, so space used is proportional to the input list.

Which Approach is Fastest?

json.dumps() is optimized for conversion and faster than manual string building or using str().

ApproachTimeSpaceBest For
json.dumps()O(n)O(n)Converting list to valid JSON string
json.dump() to fileO(n)O(1) or O(n) depending on file bufferingSaving JSON directly to file
str() conversionO(n)O(n)Quick string but not valid JSON
💡
Always use json.dumps() to get a valid JSON string from a Python list.
⚠️
Using str() instead of json.dumps() creates a string that is not valid JSON.