python remove special chars in JSON string

o
oakhill
楼主 (未名空间)

a JSON string like:
{
"a":"{\"type\":\"int\", \"value\":2}"
}

want to convert it to:
{
"a":"{\\"type\\":\\"int\\", \\"value\\":2}"
}

OR

{
"a":"{type:int, value:2}"
}

how to do it, thanks!
m
meadude

>>> js = {
... "a":"{"type":"int", "value":2}"
... }
>>>
>>> js['a']
'{"type":"int", "value":2}'
>>>
>>> str(js['a']).replace('"', '')
'{type:int, value:2}'
>>>
>>> js['a'] = str(js['a']).replace('"', '')
>>>
>>> js
{'a': '{type:int, value:2}'}
>>>
m
meadude

这个invalid:

{
"a":"{\"type\":\"int\", \"value\":2}"
}

See https://jsonchecker.com
m
meadude

到 stack overflow 问会快很多