Search code examples
jsonpython-3.xdouble-quotes

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

  • 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'>