JSON dump for User defined class in python
<p>Here is how I want my data to be : (key=name, value=[dob,[misc1, misc2,..]])</p>

<pre><code>    # my sample code
inputNames = [
('james', ['1990-01-19', ['james1', 'james2', 'james3'] ]),
('julie', ['1991-08-07', ['julie1', 'julie2'] ]),
('mikey', ['1989-01-23', ['mikey1'] ]),
('sarah', ['1988-02-05', ['sarah1', 'sarah2', 'sarah3', 'sarah4'] ])
]
class empData (list):
    def __init__ (self, misc=None):
            list.__init__([])
            # print('add empdata: ',misc[0],misc[1])
            self.dob = misc[0]
            self.extend(misc[1])
    def edprint(self):
            return(self.dob, self)
class myEmp():
    def __init__ (self, anm, amisc=None):
            self.nm = anm
            self.details = empData(amisc)
    def printme(self):
            print(self.nm, self.details.edprint())

emps={}
for i in inputNames:
    m = myEmp(i[0],i[1])
    emps[m] = m
print(emps) 
# prints addresses of variables

# for actual data use the following lines
for ea in emps:    
    emps[ea].printme() 

try:    
   with open('data.json','w') as wfd:
             json.dump(emps, wfd)              
except IOError as ioerr:
            print('File error: ',str(ioerr))
wfd.close()
</code></pre>

<p>The above gives me an error: TypeError: key &lt;<strong>main</strong>.myEmp object at 0x10143d588> is not a string
I am unable to figure out how to dump my dict of myEmp data structures as JSON</p>

<p>Before you can dump to <code>json</code> you need explicitly convert your data to a serializable type like <code>dict</code> or <code>list</code>. You could do this using a list comprehension:</p>

<pre><code>&gt;&gt;&gt; d = [{'key':ea.nm, 'value':[ea.details.dob, ea.details]} for ea in emps]
&gt;&gt;&gt; json.dumps(d)
'[{"value": ["1991-08-07", ["julie1", "julie2"]], "key": "julie"}, {"value": ["1989-01-23", ["mikey1"]], "key": "mikey"}, {"value": ["1990-01-19", ["james1", "james2", "james3"]], "key": "james"}, {"value": ["1988-02-05", ["sarah1", "sarah2", "sarah3", "sarah4"]], "key": "sarah"}]'
</code></pre>

python json dictionary dump user-defined-types