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

3.3k Views Asked by At
        f = urlopen('http://api.wunderground.com/api/API_KEY_HERE/geolookup/conditions/q/CA/LosAngeles.json')
        str_response = f.readline().decode('utf-8')
        parsed_json = json.loads(str_response)
        location = parsed_json['location']['city']
        temp_f = parsed_json['current_observation']['temp_f']
        print ("Current temperature is:", temp_f, " degrees Fahrenheit")
        precep = parsed_json['current_observation']['precep_today_in']
        print("Current wind speed is:", precep)
        wind = parsed_json['current_observation']['wind_mph']
        print("Current wind speed is:", wind)
        gust = parsed_json['current_observation']['wind_gust_mph']
        print("Current wind gust speed:", gust)
        f.close()

I saw this JSONDecodeError: Expecting value: line 1 column 1 and I am unable to figure out how to add this piece of code in.

Here is the error: enter image description here

Here is my API part as requested:

relative_humidity   "81%"
wind_string "Calm"
wind_dir    "NNE"
wind_degrees    23
wind_mph    0
wind_gust_mph   0
wind_kph    0
wind_gust_kph   0
pressure_mb "1012"
pressure_in "29.87"
pressure_trend  "-"
dewpoint_string "53 F (12 C)"
dewpoint_f  53
dewpoint_c  12
heat_index_string   "NA"
heat_index_f    "NA"
heat_index_c    "NA"
windchill_string    "NA"
windchill_f "NA"
windchill_c "NA"
1

There are 1 best solutions below

0
On BEST ANSWER

With f.readline(), your code only reads the first line that the API returns, which happens to be a blank line, so the JSON encoder complains about no data.

Change f.readline().decode('utf-8') to f.read().decode('utf-8'), and you should get past this error.