skip to Main Content

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.

2

Answers


  1. The "details" value appears to be JSON – a string encoding of the dictionary you want. When the outer dict is also encoded, its 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"])
    
    Login or Signup to reply.
  2. 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)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search