1

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)
5
  • 1
    If that file really contains {u'completed': True, u'entries' ... then it's not JSON. Commented May 17, 2017 at 15:12
  • And why is that so? Commented May 18, 2017 at 5:18
  • Because it violates the syntax rules for JSON? json.org Commented May 18, 2017 at 5:52
  • No this is for unicode encoding actually.The u represents unicode Commented May 18, 2017 at 6:06
  • It represents Unicode strings in Python source code, which is something completely different than JSON. It is invalid in JSON. Commented May 18, 2017 at 7:52

1 Answer 1

1

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'}]}
>>> 
Sign up to request clarification or add additional context in comments.

1 Comment

The only problem is each JSON object is very huge and spanning about 25 lines each. so putting it like this in a single line will be impossible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.