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