title: How to convert string int JSON into real int with json.loads
body: <p>I'm trying to convert a string which represents a JSON object to a real JSON object using <code>json.loads</code> but it doesn't convert the integers:</p>
  
  <p>(in the initial string, integers are always strings)</p>
  
  <pre><code>$&gt; python
  Python 2.7.9 (default, Aug 29 2016, 16:00:38)
  [GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; import json
  &gt;&gt;&gt; c = '{"value": "42"}'
  &gt;&gt;&gt; json_object = json.loads(c, parse_int=int)
  &gt;&gt;&gt; json_object
  {u'value': u'42'}
  &gt;&gt;&gt; json_object['value']
  u'42'
  &gt;&gt;&gt;
  </code></pre>
  
  <p>Instead of <code>{u'value': u'42'}</code> I'd like it becomes <code>{u'value': 42}</code>. I know I can run through the whole object, but I don't want to do that, it's not really efficient to do it manually, since this <code>parse_int</code> argument exists (<a href="https://docs.python.org/2/library/json.html#json.loads" rel="noreferrer">https://docs.python.org/2/library/json.html#json.loads</a>). </p>
  
  <p>Thanks to Pierce's proposition:</p>
  
  <pre><code>Python 2.7.9 (default, Aug 29 2016, 16:00:38)
  [GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; import json
  &gt;&gt;&gt;
  &gt;&gt;&gt; class Decoder(json.JSONDecoder):
  ...     def decode(self, s):
  ...         result = super(Decoder, self).decode(s)
  ...         return self._decode(result)
  ...     def _decode(self, o):
  ...         if isinstance(o, str) or isinstance(o, unicode):
  ...             try:
  ...                 return int(o)
  ...             except ValueError:
  ...                 try:
  ...                     return float(o)
  ...                 except ValueError:
  ...                     return o
  ...         elif isinstance(o, dict):
  ...             return {k: self._decode(v) for k, v in o.items()}
  ...         elif isinstance(o, list):
  ...             return [self._decode(v) for v in o]
  ...         else:
  ...             return o
  ...
  &gt;&gt;&gt;
  &gt;&gt;&gt; c = '{"value": "42", "test": "lolol", "abc": "43.4",  "dcf": 12, "xdf": 12.4}'
  &gt;&gt;&gt; json.loads(c, cls=Decoder)
  {u'test': u'lolol', u'dcf': 12, u'abc': 43.4, u'value': 42, u'xdf': 12.4}
  </code></pre>