Serialising an Enum member to JSON
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Python's json.dumps() cannot serialize Enum members by default — it raises TypeError: Object of type Color is not JSON serializable. The fix is to use a custom JSONEncoder that converts enum members to their value (or name), use enum.value explicitly before serialization, or define enums that inherit from str or int alongside Enum (e.g., class Color(str, Enum)) so they are natively JSON-serializable. This article covers all approaches.
The Problem
json.dumps() only handles basic Python types (dict, list, str, int, float, bool, None). Enum members are not in this set.
Fix 1: Custom JSONEncoder
Override default() in a JSONEncoder subclass to handle enum members. Pass cls=EnumEncoder to json.dumps().
Fix 2: str/int Mixin (Recommended for Python 3.11+)
IntEnum and StrEnum members are instances of int and str respectively, so json.dumps() handles them without a custom encoder.
Fix 3: Using default Parameter
The default parameter is simpler than subclassing JSONEncoder when you only need to handle a few types.
Fix 4: Pydantic Models
Pydantic handles enum serialization and deserialization automatically when enum members inherit from str or int.
Deserialization (JSON to Enum)
Use object_hook in json.loads() to convert raw values back into enum members during deserialization.
Multiple Enum Types
Common Pitfalls
- Using
str(enum_member)instead of.valueor.name:str(Color.RED)returns"Color.RED", not"RED"or1. Use.valuefor the underlying value or.namefor the member name. IntEnummembers compare equal to plain ints:Priority.HIGH == 3isTruewithIntEnum. This can cause subtle bugs if you rely on type-checking. Use regularEnumif you want strict type separation.- Forgetting deserialization: Serializing enums to JSON is only half the problem. You also need a strategy to convert raw JSON values back to enum members when loading data.
- Mixed enum types in one payload: A generic
default=lambda o: o.valueworks for serialization, but deserialization requires knowing which value maps to which enum type. Include type information in the JSON if needed. StrEnumbehavior differences:StrEnummembers are actual strings and can be compared with==to plain strings. This is convenient but meansisinstance(Status.ACTIVE, str)returnsTrue, which may surprise code that type-checks.
Summary
IntEnumandStrEnum(Python 3.11+) serialize natively withjson.dumps()- Custom
JSONEncoderwithdefault()handles any enum type - Use
.valuefor the underlying value,.namefor the member name default=lambda o: o.valueis the simplest approach for quick serialization- Pydantic models handle enum serialization and deserialization automatically
- For round-trip fidelity, include the enum class name in the JSON payload
Related reading
- Serializing Sqlite3 in Python
- Serve trained Tensorflow model with REST API using Flask?
- Set Cover or Hitting Set; Numpy, Least element combinations to make up full set
- Set Matplotlib colorbar size to match graph
- set_model missing 1 required positional argument 'model
- Set Popping Python
- Set Python Pub/Sub asynchronous pull subscriber threads count
- Set up Python simpleHTTPserver on Windows
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.