'json load fails because of escape double quots

test ='{"desc": "{\"OS\":\"N\",\"DR\":\"N\",\"SNAPSHOT\":\"N\",\"SERVICENAME\":\" MariaDB \"}"}'
test2 = json.loads(test)

json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 13 (char 12)

How can I get out of this hell?



Solution 1:[1]

Possible solution is the following:

test ='{"desc": "{\"OS\":\"N\",\"DR\":\"N\",\"SNAPSHOT\":\"N\",\"SERVICENAME\":\" MariaDB \"}"}'

test2 = test.replace('"{', '{').replace('}"', '}')
test2 = eval(test2)

print(test2)
print(type(test2))

Prints

{'desc': {'OS': 'N', 'DR': 'N', 'SNAPSHOT': 'N', 'SERVICENAME': ' MariaDB '}}
<class 'dict'>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gremur