0

Here is the content of my JSON file

cat ./myfile.json

{u'Records': [{u'eventVersion': u'2.0', }]}

How do I read this JSON file?

I tried reading the file with the following code,

def Read_json_file(jsonFile):
   jsonDy = {}
   if os.path.exists(jsonFile):
      with open(jsonFile, 'rt') as fin:
         jsonDy = json.load(fin)
   else:
      print("JSON file not available ->",
                                 jsonFile)
      sys.exit(1)
   print("jsonDy -> ", jsonDy)

But getting the following error,

Traceback (most recent call last):
  File "a.py", line 125, in <module>
    Main()
  File "a.py", line 18, in Main
    content = Read_json_file(eventFile)
  File "a.py", line 44, in Read_json_file
    jsonDy = json.load(fin)
  File "/usr/lib64/python2.7/json/__init__.py", line 290, in load
    **kw)
  File "/usr/lib64/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.7/json/decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python2.7/json/decoder.py", line 381, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)

What I understand is here u' represents the unicode notation, but not sure how to read this file

PS : I am using Python 2.7

1
  • 1
    Try json.loads() instead. Commented Aug 2, 2016 at 17:42

2 Answers 2

3

That's not a valid JSON structure. It's a string representation of Python data structure. The appropriate JSON structure would be:

{"Records": [{"eventVersion": "2.0"}]}

It looks like something is writing the JSON with the output of json.loads instead of json.dumps.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this,

import simplejson as json
w = json.dumps({u'Records': [{u'eventVersion': u'2.0', }]})
print json.loads(w)

or use:

import json
w = json.dumps({u'Records': [{u'eventVersion': u'2.0', }]})
print json.loads(w)

I have dumped to json to recreate the issue. You can just use json.loads

4 Comments

Here is my current code ; with open(jsonFile, 'rt') as fin: jsonDy = json.loads(fin.read())...but getting the following error...Traceback (most recent call last): content = Read_json_file(eventFile) jsonDy = json.loads(fin.read()) obj, end = self.scan_once(s, idx) ValueError: Expecting property name: line 1 column 2 (char 1)
json.loads(fin.read().replace("\'", '"')) Can you try this and tell me?
Tried that....But, Getting the same error ; For the moment ; I have changed my file to have a valid JSON content...as @Lex have pointed out...that resolved my current road block with testing...
Might be helpful to someone in the same scenario...although not sure if this is an elegant solution. This works though....json.loads(fin.read().replace("u'", '"').replace("'", '"'))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.