title: Serializing a Python namedtuple to json
body: <p>What is the recommended way of serializing a <a href="http://docs.python.org/dev/library/collections.html#collections.namedtuple"><code>namedtuple</code></a> to json with the field names retained?</p>
  
  <p>Serializing a <code>namedtuple</code> 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:</p>
  
  <pre><code>class foobar(namedtuple('f', 'foo, bar')):
      __slots__ = ()
      def __iter__(self):
          yield self._asdict()
  </code></pre>
  
  <p>The above serializes to json as I expect and behaves as <code>namedtuple</code> in other places I use (attribute access etc.,) except with a non-tuple like results while iterating it (which fine for my use case).</p>
  
  <p>What is the "correct way" of converting to json with the field names retained?</p>