JSON like string with unicode to valid JSON

2019-09-06 19:35发布

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.

2条回答
Luminary・发光体
2楼-- · 2019-09-06 19:59

JSON spec only allows javascript data (true, false for booleans, null, undefined for None properties, etc)
The string of this question, it's an python object, so as @florian-dreschsler says, you must use literal_eval from the ast module

>>> import ast
>>> json_string = """
... {u'key':[{
...        u'key':u'object',
...        u'something':u'd\xfcabc',
...        u'more':u'\u2023more',
...        u'boolean':True,  #this property fails with json module
...        u'null':None,     #this property too
...     }]
... }
... """
>>> ast.literal_eval(json_string)
{u'key': [{u'boolean': True, u'null': None, u'something': u'd\xfcabc', u'key': u'object', u'more': u'\u2023more'}]}
查看更多
男人必须洒脱
3楼-- · 2019-09-06 20:07

You can use literal_eval from the ast module for this.

ast.literal_eval(yourString)

You can then convert this Object back to JSON.

查看更多
登录 后发表回答