Serializing a Python namedtuple to json
问题 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 values being serialized and the field names being lost in translation. I would like the fields also to be retained when json-ized and hence did the following: class foobar(namedtuple('f', 'foo, bar')): __slots__ = () def __iter__(self): yield self._asdict() The above serializes to json as I expect and behaves as namedtuple in other places I use