I get a string which resembles JSON and I'm trying to convert it to valid JSON using python. It looks like this example, but the real data gets very long:
{u'key':[{
u'key':u'object',
u'something':u'd\xfcabc',
u'more':u'\u2023more',
u'boolean':True
}]
}
So there are also a lot of special characters, as well as the "wrong" boolean which should be just lowercase letters. I don't have any influence over the data I get, I just have to parse it somehow and extract some stuff from it. I tried to replace the special characters and everything and force it to be a valid JSON, but it is not at all elegant and I could easily forget to replace one type of special character.
JSON spec only allows javascript data (
true
,false
for booleans,null
,undefined
forNone
properties, etc)The string of this question, it's an python object, so as @florian-dreschsler says, you must use
literal_eval
from theast
moduleYou can use
literal_eval
from theast
module for this.ast.literal_eval(yourString)
You can then convert this Object back to JSON.