Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm loading in the following json that is contained in a text file:

{
"data sources" : [
"http://www.gcmap.com/" 
] ,
"metros" : [
{
"code" : "SCL" ,
"name" : "Santiago" ,
"country" : "CL" ,
"continent" : "South America" ,
"timezone" : -4 ,
"coordinates" : {"S" : 33, "W" : 71} ,
"population" : 6000000 ,
"region" : 1
} , {
"code" : "LIM" ,
"name" : "Lima" ,
"country" : "PE" ,
"continent" : "South America" ,
"timezone" : -5 ,
"coordinates" : {"S" : 12, "W" : 77} ,
"population" : 9050000 ,
"region" : 1
} ]}

And then I put this file in the same directory as the python file that will open it and use the following code:

import json

json_file = open('json.txt')
data = json.load(json_file)
json_file.close()

print (data)

However this gives me an error:

Traceback (most recent call last):
  File "/Users/tylerharrington/Desktop/workspace/Assignment2/src/cs242assignment2/UserInterface.py", line 11, in <module>
    data = json.load(json_file)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/json/__init__.py", line 264, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/json/__init__.py", line 309, in loads
    return _default_decoder.decode(s)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/json/decoder.py", line 352, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/json/decoder.py", line 368, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting property name enclosed in double quotes: line 1 column 1 (char 1)

Does this indicate an error with the python file or and error in my code?

share|improve this question
2  
The code is perfectly working with the data file above – esaelPsnoroMoN Feb 21 at 17:03
I created a text file called json.txt and then copied/pasted your code into my interpreter and it works just fine – brian buck Feb 21 at 17:08

1 Answer

up vote 0 down vote accepted
Expecting property name enclosed in double quotes: line 1 column 1 (char 1)

Your file probably has some invalid character at the first line just after the opening brace {

Dump the file with od to check the file content or in windows examine the file with a hex editor

Or simply run the following code snippet with your file content

with open('json.txt') as fin:
    for line in fin:
        print [hex(ord(e)) for e in line]

This will give you a fair idea about the offending character

or simply just

hex(ord(open('test.txt').read(2)[1]))
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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