0

I'm relatively new to python and completely new to json files, I'm trying to follow the simple example below:

import json

# Load the data into a list
filename = 'population_data.json'
with open(filename) as f:
    pop_data = json.load(f)

When I try to download the data found here:

https://github.com/ehmatthes/pcc/blob/master/chapter_16/population_data.json

I get the following error:

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I'm using pycharm in windows 10. Could it be related to the way that I am downloading the json file from GitHub? I download it as a txt file then resave it as a Json file it appears to be correct but clearly the code is not working. If this is not correct, what is the correct way to download a json file from Githib?

Thanks

Baz

Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
Bazman
  • 2,058
  • 9
  • 45
  • 65
  • Loading it from the webage circumvents the problem, see here: https://stackoverflow.com/questions/12965203/how-to-get-json-from-webpage-into-python-script – Bazman May 29 '17 at 17:43
  • Loading it from the webage circumvents the problem, see here: https://stackoverflow.com/questions/12965203/how-to-get-json-from-webpage-into-python-script. I'd still like to know how to save the file properly though and hence avoid the need for the urllib. – Bazman May 29 '17 at 18:00

2 Answers2

3

The URL you use returns an HTML document. This can not be parsed as a JSON document. Use download link https://raw.githubusercontent.com/ehmatthes/pcc/master/chapter_16/population_data.json to get raw content.

Setop
  • 2,262
  • 13
  • 28
  • I was using the download link, sorry that was not clear. When I click on it nothing downloads it just takes me to a webpage with the data in it. I then try to 'save as' a json file. Thing is json is not offered as a file extension option so I have to force it by typing ".json" at the end. This seems to work it then shows as a json file in windows explorer. This is already the situation I am in above. So it still does not work with the same error code. – Bazman May 29 '17 at 17:36
0

It sounds like your clicking the download link without holding "alt" first. If you click the download link without holding the "alt" key you will be taken to a website with the data which won't be easily saved as json file.

If you simply hold "alt" well right clicking on the download link you will be able to download the file as is (as a json in this case). I just downloaded the file your talking about from the link you provided as a json using this method and it worked for me.

Gino
  • 1,043
  • 16
  • 27