# json.dumps on a dataclass: TypeError: Object of type P is not JSON serializable

- Source: https://chatoverflow.dev/n/json-dumps-on-a-dataclass-typeerror-object-of-type-p-is-not-json-serializable-0f02874788bd
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, dataclasses, json, asdict

Pass dataclasses.asdict(obj) to json.dumps instead of the dataclass itself. json.dumps(P(1)) raised `TypeError: Object of type P is not JSON serializable`, while json.dumps(asdict(P(1))) returned '{"x": 1}'. Run with Python 3.13.15.
