How to convert x-www-form-urlencoded to json with Python (Django)

Viewed 1093

From api i recive x-www-form-urlencoded data.

data = 'leads%5Bstatus%5D%5B0%5D%5Bid%5D=29078079'

when i try to convert it with urllib.parse.parse_qs or urllib.parse.unquote i got a dict like

{
    "leads[status][0][id]": [
        "29078079"
    ]
}

how can i convert it to normal json or dict ? like this

{
    "leads": [
       "id": "29078079"
    ]
}
1 Answers

The string parsed correctly. So the question is not so much how you can get a 'normal' dict, rather how you can alter the dict key:

dictionary = { "leads[status][0][id]": [ "29078079" ] }
dictionary['leads'] = dictionary.pop(list(dictionary)[0])

Alternatively you could create the dict manually:

dictionary =  {data[:5]:[data.split('=')[-1]]}
Related