How to specify the level of json.loads()?
<p>I have a JSON string to be loaded:</p>

<pre><code>{"a": 1, "b": {"c": 123} }
</code></pre>

<p>If I use <code>json.loads</code>, it will load <code>{"c": 123}</code> as a <code>dict</code>. However, I don't want that to happen.</p>

<p>I want to be able to access the string directly, without interpreting the internal object in to a dict, like so:</p>

<p><code>json_dict['b'] == '{"c": 123}'</code></p>

<p>rather than,</p>

<p><code>json_dict['b'] == {"c": 123}</code></p>

<p>since the format (like spaces) and order (which will be random in a <code>dict</code>) may change and the exact string is needed to do an RSA verification.</p>

<p>The actual code follows:</p>

<pre><code>r = requests.get('https://openapi.alipaydev.com/gateway.do', params)
print(r.text) 
response = json.loads(r.text)
print(response)
alipay_response = response['alipay_trade_precreate_response']
print(alipay_response['code'])
</code></pre>

<p>The output is:</p>

<pre><code>{"alipay_trade_precreate_response":{"code":"10000","msg":"Success","out_trade_no":"123","qr_code":"https:\/\/qr.alipay.com\/bax08377e7kxveupoqnt001c"},"sign":"EqocoROqXbpkGdkFZakEkoOymGS7+UcvNi1YmcQffF4wtyQcj/RTO1sLHY8tWZFx0rxQAPjkX+7Hrszn4pNWkuBbM/c88oEbxYc+pCvnF49SHZmfkBqY6eJlLIHgPHXus5KFtvlMmkzANNHmD7c72FLDAbvMHKVyEcRPkU9ANIk="}
{'sign': 'EqocoROqXbpkGdkFZakEkoOymGS7+UcvNi1YmcQffF4wtyQcj/RTO1sLHY8tWZFx0rxQAPjkX+7Hrszn4pNWkuBbM/c88oEbxYc+pCvnF49SHZmfkBqY6eJlLIHgPHXus5KFtvlMmkzANNHmD7c72FLDAbvMHKVyEcRPkU9ANIk=', 'alipay_trade_precreate_response': {'out_trade_no': '123', 'qr_code': 'https://qr.alipay.com/bax08377e7kxveupoqnt001c', 'msg': 'Success', 'code': '10000'}}
10000
</code></pre>

<p>We can see that <code>response['alipay_trade_precreate_response']</code> is a <code>dict</code>, and the order is different from the string. I need the string so I can verify the RSA signature.</p>

<p>You have one solution using json.loads object_hook parameter.</p>

<p>object_hook is an optional function that will be called with the result of any object literal decoded (a dict). The return value of object_hook will be used instead of the dict. This feature can be used to implement custom decoders (e.g. JSON-RPC class hinting).</p>

<pre><code>def phook(x):
    for item in x:
        if isinstance(x[item], dict):
            x[item] = json.dumps(x[item])         
    return x

x = '{"a": 1, "b": {"c": 123}}'
json.loads(x, object_hook=phook)

# the result will be 
# {u'a': 1, u'b': '{"c": 123}'}
</code></pre>

python json