12

I am trying to load a json file in python. My json file looks like this:

{'awesomeness': 2.5, 'party': 'Republican', 'Age': 50, 'State': 'California', 'Ideology': 0.5,  
'time': {'day': 20, 'mon': 2, 'sec': 3}, 'overall': 9.5, 'review': 'Best Senator ever\tPretty balanced 
view.\tNot sure if he can get reelected'}
{'awesomeness': 0.5, 'party': 'Republican', 'Age': 70, 'State': 'New York', 'Ideology': 0.8,  
'time': {'day': 25, 'mon': 8, 'sec': 31}, 'overall': 5.5, 'review': 'NA'}

This is my code.

with open("senator.json") as json_file:
  data = json.load(json_file)

But I got following error,

File "<stdin>", line 1, in <module>
File "<string>", line 2, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 290, in load
**kw)
 File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
 File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
 File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/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)

Why do I get this error and how can I load this file? Thanks for any help!

2
  • 1
    This might have something to do with your JSON being in single quotes. Valid JSON is with double quotes. Try putting your JSON through a beautifier (jsonformatter.curiousconcept.com) and if it comes back with errors, then there's your problem.
    – Jordan
    Commented Jan 8, 2015 at 5:25
  • Also to tag a solution onto here. If you want your file to be json, you could use ast.literal_eval to load it once, and json.dumps(value) into the file again to make it into valid JSON. literal_eval shouldn't be used to load strings like this, so do yourself a favor and convert it - it will save you headaches down the road
    – Jordan
    Commented Jan 8, 2015 at 5:38

4 Answers 4

13

Your JSON is illegal, as the other folks already pointed out. This is why the Python JSON parser if falling flat out. Your JSON should look like this:

[
  {
    "awesomeness": 2.5,
    "party": "Republican",
    "Age": 50,
    "State": "California",
    "Ideology": 0.5,
    "time": {
        "day": 20,
        "mon": 2,
        "sec": 3
    },
    "overall": 9.5,
    "review": "Best Senator ever \t Pretty balanced view. \t Not sure if he can get reelected"
  },
  {
    "awesomeness": 0.5,
    "party": "Republican",
    "Age": 70,
    "State": "NewYork",
    "Ideology": 0.8,
    "time": {
        "day": 25,
        "mon": 8,
        "sec": 31
    },
    "overall": 5.5,
    "review": "NA"
  }
]

Notice the double quotes for strings instead of single quotes (which are illegal to denote a string inside JSON) and wrapping your two objects {...} in square brackets so they are inside of an array.

The latter is needed because JSON is hierarchical. Your posted JSON code simply has two objects side-by-side without any outer structure. In that case, if you thus leave the square brackets off (no array), then the JSON parser will suddenly encounter a comma , after your first object when it in fact expected an EOF.

Now if you try to run your code again:

with open('senator.json', 'r') as json_file:
  data = json.load(json_file)

You should be able to read out the file without any errors.

1

JSON strings are delimited by double quotes, not single. Your input isn't true JSON.

2
  • And what about the second part of the question?
    – alecxe
    Commented Jan 8, 2015 at 5:33
  • You could either (a) change the file to use true JSON format, by editing it or sed or whatever means, in which case your current code will work; or (b) you can read the current file into a Python string, and use replace to change single quotes to double (being careful of escaped embedded single quotes) then using json.loads to load from that changed string. Commented Jan 9, 2015 at 12:52
1

Your json file is not in proper json format...I think it should be like this

{'awesomeness': 2.5, 'party': 'Republican', 'Age': 50, 'State': 'California', 'Ideology': 0.5,'time': {'day': 20, 'mon': 2, 'sec': 3}, 'overall': 9.5, 'review': 'Best Senator ever\tPretty balanced w.\tNot sure if he can get reelected','awesomeness': 0.5, 'party': 'Republican', 'Age': 70, 'State': 'New York', 'Ideology': 0.8,'time': {'day': 25, 'mon': 8, 'sec': 31}, 'overall': 5.5, 'review': 'NA'}

And you should read the file.Syntax to read is

with open("senator.json","r") as json_file:
     data = json.load(json_file)
2
  • 2
    This is not valid JSON. Valid JSON is with double quotes.
    – Jordan
    Commented Jan 8, 2015 at 5:50
  • Wait really? JSON needs double quotes?
    – Tim Reilly
    Commented Apr 2, 2018 at 20:44
-2

I found another way to deal with this single quote problem. I used for loop and eval to read this file.

This is the code I used.

def getdata(file):
  for l in open(file):
    yield eval(l)

thedata = list(getdata('filename.json')

Thanks everyone for letting me know the problem!

2
  • 4
    Please do know that eval is evil. This sounds as an ugly and dangerous workaround for a problem that should be fixed within the JSON itself (or the code that generates the JSON). You never know which arbitrary Python code is actually lurking inside your JSON file which you now blindly execute...
    – Timusan
    Commented Jan 8, 2015 at 6:32
  • 1
    ast.literal_eval works fine for strings, and it won't blow up your computer. Commented Jan 25, 2020 at 20:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.