Possible Duplicate:
Python JSON serialize a Decimal object
Task: to convert a dict containing a mix of data-types (integer/strint/decimal/...) as values, into JSON array.
I know how to convert a python dict into JSON:
D = {key1:val1, key2,val2}
import json
jD = json.dumps(D)
I also know that decimal value has to be converted into a string, otherwise python will throw error that 'decimal is not json serializable'.
So I would need to iterate through the dict to look for the data-type.
for key in D.keys():
if type(key) == '<class "decimal.Decimal">': ## this is erroneous. pl. suggest correction
D[key] = str(D[key])
But this programming is involving hand-coding and hard-coding.
If I get a nested dictionary structure, again hard-coding would be required (which is wrong).
Is there any other method/trick to get JSON array from whatever data-type in dict?