What is the recommended way of serializing a namedtuple to json with the field names retained?
Serializing a namedtuple to json results in only the valu
namedtuple
If it's just one namedtuple you're looking to serialize, using its _asdict() method will work (with Python >= 2.7)
_asdict()
>>> from collections import namedtuple >>> import json >>> FB = namedtuple("FB", ("foo", "bar")) >>> fb = FB(123, 456) >>> json.dumps(fb._asdict()) '{"foo": 123, "bar": 456}'