Json Like String With Unicode To Valid Json
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':
Solution 1:
You can use literal_eval
from the ast
module for this.
ast.literal_eval(yourString)
You can then convert this Object back to JSON.
Solution 2:
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'}]}
Post a Comment for "Json Like String With Unicode To Valid Json"