is in python a deep json parsing?

65 views Asked by At

I have next code:

import json
string = b'{"campaign_id":6,"email":"[email protected]","time":"2024-01-26T06:05:59.852155Z","message":"Submitted Data","details":"{\\"payload\\":{\\"__RequestVerificationToken\\":[\\"CfDJ8EfyW-fedawawdawdwadwadwawadawd-6xqg_ZIdEJBpbAHkDQdNM\\"],\\"__original_url\\":[\\"https://test-test-test.ru/\\"],\\"password\\":[\\"test\\"],\\"qwe\\":[\\"Gg4gTWg\\"],\\"username\\":[\\"test\\"]},\\"browser\\":{\\"address\\":\\"1.1.1.1\\",\\"user-agent\\":\\"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0\\"}}"}'
a = json.loads(string)

Problem is not parsing dict by key details. If you make json.loads, you will see that in key value the inside json cannot be parsed. How can i parse dict in key "details".

I tried some another librirais. But no success.

3

There are 3 answers

0
Mahboob Nur On

The "details" key contains a string representation of a JSON object, which is escaped. To parse the nested JSON inside the "details" key,

  • you need to first decode the string and then
  • parse it as JSON.

Try like this

import json

string = b'{"campaign_id":6,"email":"[email protected]","time":"2024-01-26T06:05:59.852155Z","message":"Submitted Data","details":"{\\"payload\\":{\\"__RequestVerificationToken\\":[\\"CfDJ8EfyW-fedawawdawdwadwadwawadawd-6xqg_ZIdEJBpbAHkDQdNM\\"],\\"__original_url\\":[\\"https://test-test-test.ru/\\"],\\"password\\":[\\"test\\"],\\"qwe\\":[\\"Gg4gTWg\\"],\\"username\\":[\\"test\\"]},\\"browser\\":{\\"address\\":\\"1.1.1.1\\",\\"user-agent\\":\\"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0\\"}}"}'
decoded_string = string.decode('utf-8')
data = json.loads(decoded_string)
details_json = json.loads(data['details'])

print(details_json)
0
tdelaney On

The "details" value appears to be JSON - a string encoding of the dictionary you want. When the outer dict is also encoded, it's just a normal string as far as the encoder is concern. You can unwrap by reversing the steps: Decode the outer dict, then decode the inner.

import json
string = b'{"campaign_id":6,"email":"[email protected]","time":"2024-01-26T06:05:59.852155Z","message":"Submitted Data","details":"{\\"payload\\":{\\"__RequestVerificationToken\\":[\\"CfDJ8EfyW-fedawawdawdwadwadwawadawd-6xqg_ZIdEJBpbAHkDQdNM\\"],\\"__original_url\\":[\\"https://test-test-test.ru/\\"],\\"password\\":[\\"test\\"],\\"qwe\\":[\\"Gg4gTWg\\"],\\"username\\":[\\"test\\"]},\\"browser\\":{\\"address\\":\\"1.1.1.1\\",\\"user-agent\\":\\"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0\\"}}"}'
a = json.loads(string)
a["details"] = json.loads(a["details"])
0
Andrei Gurko On

May be this solution can help you call ones loads function

import json


class InnerDictEncoder(json.JSONDecoder):
    def __init__(self, *args, **kwargs):
        json.JSONDecoder.__init__(self, object_hook=self.object_hook, *args, **kwargs)

    def object_hook(self, dct):
        if 'details' in dct:
            dct['details'] = json.loads(dct['details'])
            return dct
        return dct



string = b'{"campaign_id":6,"email":"[email protected]","time":"2024-01-26T06:05:59.852155Z","message":"Submitted Data","details":"{\\"payload\\":{\\"__RequestVerificationToken\\":[\\"CfDJ8EfyW-fedawawdawdwadwadwawadawd-6xqg_ZIdEJBpbAHkDQdNM\\"],\\"__original_url\\":[\\"https://test-test-test.ru/\\"],\\"password\\":[\\"test\\"],\\"qwe\\":[\\"Gg4gTWg\\"],\\"username\\":[\\"test\\"]},\\"browser\\":{\\"address\\":\\"1.1.1.1\\",\\"user-agent\\":\\"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0\\"}}"}'.decode(encoding='utf-8')
a = json.loads(string, cls=InnerDictEncoder)

print(a)