JSONDecodeError: while trying to read a JSON file from Github

Question:

Trying to make a script to read this json file and I having some erros while trying it.

urllib.request.urlopen("https://github.com/lutangar/cities.json/blob/master/cities.json") as url:
data = json.load(url)
print(data)

When I try to run, I’m having this error and I don’t know how to fix it.

JSONDecodeError: Expecting value: line 8 column 1 (char 7)

I’m trying to open the link but I alwasy got an error message.

Asked By: Tayzer Damasceno

||

Answers:

Add ?raw=true to the URL:

import urllib.request

with urllib.request.urlopen(
    "https://github.com/lutangar/cities.json/blob/master/cities.json?raw=true"
) as u:
    data = json.load(u)
    print(data)

Prints:

[{'country': 'AD', 'name': 'Sant Julià de Lòria', 'lat': '42.46372', 'lng': '1.49129'}, {'country': 'AD', 'name': 'Pas de la Casa', 'lat': '42.54277', 'lng': '1.73361'}, {'country': 'AD', 'name': 'Ordino', 'lat': '42.55623', 'lng':  ...
Answered By: Andrej Kesely
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.