Help with dress for the weather project


#1

Hi - I’ve been trying to do the dress for the weather project. https://projects.raspberrypi.org/en/projects/dress-for-the-weather/ I ran into an issue early on - the JSON list downloaded from the API was different - there is a new line after each value, while in the JSON list on github the values are all in the same line. (So when I’d do cities[0] it would return /n…) I replaced the JSON list I’d downloaded with the one on Github, which worked, and was able to convert the list to a dictionary. But I haven’t been able to go further: when I tried to test it in the shell (https://projects.raspberrypi.org/en/projects/dress-for-the-weather/7) it returned this error. Can anyone help me figure out what’s going on? Thank you!

>>> get_city_id()
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    get_city_id()
  File "/home/dress-for-the-weather/weather.py", line 8, in get_city_id
    data = [loads(line) for line in f]
  File "/home/dress-for-the-weather/weather.py", line 8, in <listcomp>
    data = [loads(line) for line in f]
  File "/home/anaconda3/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/home/anaconda3/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/home/anaconda3/lib/python3.6/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 1)

#2

Looks like whatever you’re trying to parse is not valid JSON, do you have a link to the JSON file and code you’re using?


#3

I was using this: https://github.com/raspberrypilearning/dress-for-the-weather/blob/master/code/city.list.json since the list from the Open Weather API didn’t work.


#4

That is not, as far as I can tell, valid JSON. You could parse it as such with some trickery, but any stock JSON decoder will fail.

The issue is that you have multiple objects {"_id": 12345} with no syntax denoting their relationship.

Instead of:

{"_id": 12345}
{"_id": 12345}
{"_id": 12345}

Your file should look something like:

[
{"_id": 12345},
{"_id": 12345},
{"_id": 12345}
]

You could probably open the file as plain text, parse it line by line using readline() and parse each individual line successfully as JSON if you didn’t want to modify your (huuuuggeee) country file.


#5

Thanks, I’ll give that a shot and/or try to work with the file downloaded via the API.