Home > Software design >  json load fails because of escape double quots
json load fails because of escape double quots

Time:04-19

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?

CodePudding user response:

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