Json.decoder.JSONDecodeError: Expected value: row 1 column 1 (char 0)

I am trying to import a file that was saved using json.dumps and contains the coordinates of the tweets:

 { "type": "Point", "coordinates": [ -4.62352292, 55.44787441 ] } 

My code is:

 >>> import json >>> data = json.loads('/Users/JoshuaHawley/clean1.txt') 

But every time I get an error:

 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 

I want to finish extracting all the coordinates and saving them separately to another file so that later they can be matched, but this seemingly simple problem - this prevents me from doing this. I looked at the answers to similar errors, but it seems I can not apply them to this. Any help would be appreciated as I am relatively new to python.

+8
json python
source share
2 answers

json.loads() takes a JSON encoded string, not a filename. Instead, you want to use json.load() (no s ) and pass an open file object:

 with open('/Users/JoshuaHawley/clean1.txt') as jsonfile: data = json.load(jsonfile) 

The open() command creates a file object from which json.load() can read, to create a decoded Python object for you. The with statement ensures that the file will be closed when it ends.

An alternative is to read the data yourself and then pass it to json.loads() .

+13
source share

I had a similar error: "Expected value: row 1 column 1 (char 0)"

This helped me add "myfile.seek (0)", move the pointer to character 0

 with open(storage_path, 'r') as myfile: if len(myfile.readlines()) != 0: myfile.seek(0) Bank_0 = json.load(myfile) 
0
source share

All Articles