How to Convert Dictionary to JSON in Python

If you want to turn a Python dictionary into JSON, use the built-in json module.

  • Use json.dumps() when you want a JSON string
  • Use json.dump() when you want to write JSON to a file

This page shows both methods with simple examples.

Quick answer

import json

data = {"name": "Alice", "age": 25, "active": True}
json_text = json.dumps(data)
print(json_text)

Output:

{"name": "Alice", "age": 25, "active": true}

Use json.dumps() when you want a JSON string. Use json.dump() when you want to save JSON directly to a file.

What this page helps you do

  • Convert a Python dictionary into a JSON string
  • Save dictionary data as JSON in a file
  • Understand the difference between json.dumps() and json.dump()
  • Avoid common beginner mistakes when working with JSON

When to use dictionary to JSON conversion

Convert a dictionary to JSON when:

  • Sending data to an API
  • Saving structured data to a file
  • Sharing data between Python and other languages
  • Creating text output in JSON format

If you are new to dictionaries, see what a Python dictionary is.

Convert a dictionary to a JSON string with json.dumps()

json.dumps() takes a Python object and returns a string in JSON format.

Steps:

  • Import the built-in json module
  • Pass the dictionary to json.dumps()
  • Store or print the returned string
import json

data = {
    "name": "Alice",
    "age": 25,
    "active": True
}

json_text = json.dumps(data)

print(json_text)
print(type(json_text))

Output:

{"name": "Alice", "age": 25, "active": true}
<class 'str'>

Important points:

  • data is a Python dictionary
  • json_text is a string
  • Python True becomes JSON true

Use this when you want to:

  • Print JSON
  • Send JSON in a web request
  • Return JSON from a function
  • Store JSON in a variable

For a deeper explanation, see json.dumps() explained.

Write a dictionary to a JSON file with json.dump()

json.dump() writes JSON directly into a file.

Steps:

  • Open a file in write mode
  • Pass the dictionary and file object to json.dump()
  • The JSON text is written to the file
import json

data = {
    "name": "Alice",
    "age": 25,
    "active": True
}

with open("data.json", "w") as file:
    json.dump(data, file)

This creates a file named data.json.

The file content will look like this:

{"name": "Alice", "age": 25, "active": true}

Use this when you want to save JSON to disk instead of keeping it in a Python string.

For more detail, see json.dump() explained.

Make JSON easier to read

JSON often looks better when it is formatted.

You can use:

  • indent=4 to add spacing
  • sort_keys=True to sort keys alphabetically

Pretty-print a JSON string

import json

data = {
    "name": "Alice",
    "age": 25,
    "active": True
}

json_text = json.dumps(data, indent=4, sort_keys=True)
print(json_text)

Output:

{
    "active": true,
    "age": 25,
    "name": "Alice"
}

Write formatted JSON to a file

import json

data = {
    "name": "Alice",
    "age": 25,
    "active": True
}

with open("data.json", "w") as file:
    json.dump(data, file, indent=4, sort_keys=True)

Pretty formatting is useful when:

  • Reading saved files
  • Checking your output
  • Debugging data problems

You can also read how to pretty-print JSON in Python.

How Python values map to JSON values

When you convert a dictionary to JSON, Python values are changed into JSON values.

PythonJSON
dictobject
listarray
Truetrue
Falsefalse
Nonenull
strstring
int, floatnumber

Example:

import json

data = {
    "name": "Alice",
    "skills": ["Python", "JSON"],
    "active": True,
    "manager": None
}

print(json.dumps(data, indent=4))

Output:

{
    "name": "Alice",
    "skills": [
        "Python",
        "JSON"
    ],
    "active": true,
    "manager": null
}

JSON objects are expected to use string keys. In practice, your dictionary keys should be strings when exporting to JSON.

For a broader overview, see the Python json module overview.

Values that cannot be converted directly

Some Python values are not supported by JSON by default.

Common examples:

  • set
  • Custom objects
  • Functions

This code causes an error because a set is not JSON serializable:

import json

data = {
    "name": "Alice",
    "tags": {"python", "json"}
}

print(json.dumps(data))

You will get an error like:

TypeError: Object of type set is not JSON serializable

Fix unsupported values before converting

Convert unsupported values into supported ones first.

For example, change a set into a list:

import json

data = {
    "name": "Alice",
    "tags": list({"python", "json"})
}

print(json.dumps(data, indent=4))

Output:

{
    "name": "Alice",
    "tags": [
        "python",
        "json"
    ]
}

A good rule is:

  • Convert sets to lists
  • Convert custom objects to dictionaries
  • Convert special values to strings if needed

Beginner debugging checks

If your conversion is not working, check these first:

  • Make sure your variable is really a dictionary
  • Look for unsupported data types like set
  • Print the type before converting
  • Read the full error message carefully

Useful debugging commands:

print(type(data))
print(data)
import json
print(json.dumps(data, indent=4))

Run your script from the terminal if needed:

python your_script.py

If you already have JSON text and want to turn it back into Python data, read how to parse JSON in Python.

Common mistakes

These are common beginner mistakes when converting a dictionary to JSON:

  • Using json.dump() when you wanted a string instead of a file write
  • Using json.dumps() and expecting it to save to a file automatically
  • Trying to convert unsupported types like set
  • Forgetting to import the json module
  • Opening the output file with the wrong mode

Example of a common mix-up:

import json

data = {"name": "Alice"}

# This returns a string, but does not save a file
json_text = json.dumps(data)

If you want to save to a file, use:

import json

data = {"name": "Alice"}

with open("data.json", "w") as file:
    json.dump(data, file)

FAQ

What is the difference between json.dump() and json.dumps()?

json.dump() writes JSON to a file. json.dumps() returns JSON as a string.

Can I convert a nested dictionary to JSON?

Yes. Nested dictionaries and lists can be converted as long as the values are JSON serializable.

Why does Python say an object is not JSON serializable?

It means the value is not a supported JSON type, such as a set or custom object.

How do I make JSON output readable?

Use indent=4 in json.dumps() or json.dump() to pretty-print the result.

See also