needhelp
needhelp

Reputation: 11

Read unicode encoded JSON and access it using Python

I have a very huge unicode JSON data of the following format

{u'completed': True, u'entries': [{u'absolute_time': u'2017-05-17T10:41:52Z', u'command': None, u'level': u'NORMAL',......

It has Json objects within JSON objects. Unable to read it and parse it due to the encoding. Tried the following code. Could someone please tell how to parse it and convert it to a normal JSON object.

with open(r"inp.json", 'r') as jsonData:
    jsonToPython = json.load(jsonData) #gives error here itself
    #jsonData = ast.literal_eval(jsonData)
    print(json.dumps(jsonToPython))
    #print (jsonToPython)

Upvotes: 1

Views: 153

Answers (1)

Maurice Meyer
Maurice Meyer

Reputation: 18136

You can try to load the (stringified) python object using ast:

>>> #obj = open(r"inp.json", 'r').read()
>>> obj = "{u'completed': True, u'entries': [{u'absolute_time': u'2017-05-17T10:41:52Z'}]}"
>>> ast.literal_eval(obj)
{'completed': True, 'entries': [{'absolute_time': '2017-05-17T10:41:52Z'}]}
>>> 

Upvotes: 1

Related Questions