Why does json.dumps sometimes fail to serialize my Python object? or How can I customize the output f

20 Replies, 1178 Views

Ugh, I feel your pain. json.dumps is like that one friend who only eats plain pizza.

Pro tip: Check out the `dataclasses` module if you’re on Python 3.7+. `@dataclass` objects play nice with json.dumps if you use `asdict()` from the `dataclass` module first.

Example:
```python
from dataclasses import asdict
json.dumps(asdict(your_dataclass_obj))
```

Life-changer.

Messages In This Thread



Users browsing this thread: 1 Guest(s)