How to convert Dataclass to JSON in Python [5 Ways]

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
4 min

banner

# Table of Contents

  1. How to convert Dataclass to JSON in Python
  2. Converting a Dataclass to JSON with a custom JSONEncoder
  3. Convert a Dataclass to JSON with the dataclasses_json package
  4. Converting a dataclass object to a JSON string with the default argument

# How to convert Dataclass to JSON in Python

To convert a dataclass to JSON in Python:

  1. Use the dataclasses.asdict() method to convert the dataclass to a dictionary.
  2. Pass the dictionary to the json.dumps() method.
main.py
import json from dataclasses import dataclass, asdict @dataclass class InventoryItem: name: str unit_price: float quantity_available: int = 0 item1 = InventoryItem('A', 100, 1) print(item1) print('-' * 50) to_json = json.dumps(asdict(item1)) print(to_json) print('-' * 50) print(type(to_json))
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
InventoryItem(name='A', unit_price=100, quantity_available=1) -------------------------------------------------- {"name": "A", "unit_price": 100, "quantity_available": 1} -------------------------------------------------- <class 'str'>

convert dataclass to json in python

The dataclasses.asdict() method converts the supplied dataclass instance to a dictionary.

main.py
from dataclasses import dataclass, asdict @dataclass class InventoryItem: name: str unit_price: float quantity_available: int = 0 item1 = InventoryItem('A', 100, 1) print(item1) print('-' * 50) print(asdict(item1))

Running the code sample produces the following output.

shell
InventoryItem(name='A', unit_price=100, quantity_available=1) -------------------------------------------------- {'name': 'A', 'unit_price': 100, 'quantity_available': 1}

convert dataclass to dictionary

The dictionary contains the name: value pairs of the dataclass object.

The last step is to pass the dictionary to the json.dumps() method.

main.py
import json from dataclasses import dataclass, asdict @dataclass class InventoryItem: name: str unit_price: float quantity_available: int = 0 item1 = InventoryItem('A', 100, 1) print(item1) print('-' * 50) print(json.dumps(asdict(item1)))
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
InventoryItem(name='A', unit_price=100, quantity_available=1) -------------------------------------------------- {"name": "A", "unit_price": 100, "quantity_available": 1}

The json.dumps() method converts a Python object to a JSON formatted string.

The json.dumps() method handles the conversion of a dictionary to a JSON string without any issues.

# Converting a Dataclass to JSON with a custom JSONEncoder

You can also extend the built-in JSONEncoder class to convert a dataclass object to a JSON string.

main.py
import json from dataclasses import dataclass, asdict, is_dataclass @dataclass class InventoryItem: name: str unit_price: float quantity_available: int = 0 item1 = InventoryItem('A', 100, 1) print(item1) print('-' * 50) class DCJSONEncoder(json.JSONEncoder): def default(self, o): if is_dataclass(o): return asdict(o) return super().default(o) json_str = json.dumps(item1, cls=DCJSONEncoder) print(json_str)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
InventoryItem(name='A', unit_price=100, quantity_available=1) -------------------------------------------------- {"name": "A", "unit_price": 100, "quantity_available": 1}

We extended the default JSONEncoder class to be able to handle the conversion of dataclass objects to JSON.

The JSONEncoder class supports the following objects and types by default.

PythonJSON
dictobject
list, tuplearray
strstring
int, float, int and float derived Enumsnumber
Truetrue
Falsefalse
Nonenull

By default, the JSONEncoder class doesn't support conversion of dataclass objects to JSON.

You can change the default behavior by implementing a default method.

main.py
import json from dataclasses import dataclass, asdict, is_dataclass class DCJSONEncoder(json.JSONEncoder): def default(self, o): if is_dataclass(o): return asdict(o) return super().default(o) json_str = json.dumps(item1, cls=DCJSONEncoder) print(json_str)
The code for this article is available on GitHub

If the supplied object is a dataclass object, we return the result of calling asdict() with the object.

If the object is any other type, we use the default behavior.

Notice that we have to set the cls argument to the custom JSONEncoder class when calling json.dumps().

main.py
json_str = json.dumps(item1, cls=DCJSONEncoder)

If you forget to pass the cls argument, the default JSONEncoder is used and you'd get an error.

# Convert a Dataclass to JSON with the dataclasses_json package

You can also use the dataclasses-json module to convert a dataclass object to JSON.

First, open your terminal in your project's root directory and install the package.

shell
pip install dataclasses-json # or with pip3 pip3 install dataclasses-json

install dataclasses json

Now import the module and use the dataclass_json method as a decorator.

main.py
from dataclasses import dataclass from dataclasses_json import dataclass_json @dataclass_json @dataclass class InventoryItem: name: str unit_price: float quantity_available: int = 0 item1 = InventoryItem('A', 100, 1) print(item1) print('-' * 50) json_str = item1.to_json() print(json_str)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
InventoryItem(name='A', unit_price=100, quantity_available=1) -------------------------------------------------- {"name": "A", "unit_price": 100, "quantity_available": 1}

convert dataclass to json with dataclasses json module

Notice that we used the dataclass_json method to decorate the class.

main.py
from dataclasses import dataclass from dataclasses_json import dataclass_json @dataclass_json @dataclass class InventoryItem: name: str unit_price: float quantity_available: int = 0

The next step is to call the to_json() method on the dataclass object.

main.py
json_str = item1.to_json() print(json_str)

If you need to convert the JSON string back to a dataclass object, use the InventoryItem.from_json() method.

main.py
from dataclasses import dataclass from dataclasses_json import dataclass_json @dataclass_json @dataclass class InventoryItem: name: str unit_price: float quantity_available: int = 0 item1 = InventoryItem('A', 100, 1) print(item1) print('-' * 50) json_str = item1.to_json() print(json_str) print('-' * 50) dataclass_obj = InventoryItem.from_json(json_str) print(dataclass_obj)

Running the code sample produces the following output.

shell
InventoryItem(name='A', unit_price=100, quantity_available=1) -------------------------------------------------- {"name": "A", "unit_price": 100, "quantity_available": 1} -------------------------------------------------- InventoryItem(name='A', unit_price=100.0, quantity_available=1)

Notice that I called the from_json() method on the decorated class.

main.py
dataclass_obj = InventoryItem.from_json(json_str) print(dataclass_obj)

convert dataclass to json and back

# Converting a dataclass object to a JSON string with the default argument

You can also specify the default argument when calling json.dumps() to convert a dataclass object to a JSON string.

main.py
import json from dataclasses import dataclass @dataclass class InventoryItem: name: str unit_price: float quantity_available: int = 0 item1 = InventoryItem('A', 100, 1) print(item1) print('-' * 50) json_str = json.dumps(item1, default=lambda dc: dc.__dict__) print(json_str)

using default argument

The code for this article is available on GitHub

Running the code sample produces the following output.

shell
InventoryItem(name='A', unit_price=100, quantity_available=1) -------------------------------------------------- {"name": "A", "unit_price": 100, "quantity_available": 1}

The default argument should be set to a function that gets invoked for objects that cannot be otherwise deserialized.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.