Skip to content
Advertisement

Problem with loading a json file for geo_data in python

I am currently trying to use folium library in python to create webmaps. I have a file world.json which contains geo_data. I have provided a link to the file at the end of this post. I tried the following code:

data = [json.loads(line) for line in open('world.json', 'r')]

and received the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <listcomp>
  File "C:UsersnameAppDataLocalProgramsPythonPython38libjson__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:UsersnameAppDataLocalProgramsPythonPython38libjsondecoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:UsersnameAppDataLocalProgramsPythonPython38libjsondecoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

How can I load this file?

What I want to achieve is essentially obtain the population data and create a Choropleth and overlay it on my webmap.

Edit: Forgot the link:

https://1drv.ms/u/s!Army95vqcKXpaooVAZU_g-VCAVw?e=vwTknq

Edit: Previous link to skydrive stopped working due to “high traffic”. Below is link to dropbox, hopefully this works:

https://www.dropbox.com/s/gmm8db0g03rc7cv/world.json?dl=0

Advertisement

Answer

Good news/bad news: It turns out that this file was encoded in a locale that we are not accustomed to, and json/ascii cannot make sense of some of the character encoding. I tried this, and it seems to be working for me — with a major caveat:

with open("world.json", "r") as fh:
    contents = fh.read()
asciiContents = contents.encode("ascii", errors="ignore")
data = json.loads(asciiContents)

The major caveat is that only 3 countries come through with no encoding errors:

>>> len(data["features"])
3

Maybe there another source for this data that is closer to a native english locale, or maybe someone else can provide wisdom in encoding foreign data in a more friendly way…

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement