-3

I am extremely new to Json, Phyton. But im trying to create my own weather app. I'm failing to get the weather out of this Jsonobject.

This is what the Jsonobject looks like:

{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":300,"main":"Drizzle","description":"light intensity drizzle","icon":"09d"}],"base":"stations","main":{"temp":280.32,"pressure":1012,"humidity":81,"temp_min":279.15,"temp_max":281.15},"visibility":10000,"wind":{"speed":4.1,"deg":80},"clouds":{"all":90},"dt":1485789600,"sys":{"type":1,"id":5091,"message":0.0103,"country":"GB","sunrise":1485762037,"sunset":1485794875},"id":2643743,"name":"London","cod":200}

And this is my code:

@app.route('/temperatuur', methods=['GET','POST',])
def temperatuur():
    zipcode = request.form['zip']
    r = requests.get('http://api.openweathermap.org/data/2.5/weather?zip='+zipcode+',be&APPID=84c7d83bae2f2396ebd3a4a48dfdd057')
    json_object = r.json()
    weer = json_object['weather',[1]]
    temp_k = int(json_object['main']['temp'])
    temp_c = (temp_k - 273)
    plaats = str(json_object['name'])
    return render_template('temperatuur.html', temperatuur=temp_c, plaats = plaats, weer = weer)

This is the fault:

ypeError: unhashable type: 'list'

Phil Ross
  • 25,590
  • 9
  • 67
  • 77
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Barmar May 23 '17 at 15:34

2 Answers2

0

I believe this is where you're doing wrong

weer = json_object['weather',[1]]

change this to :

weer = json_object['weather'][0]

Also, I don't think you have data object 'name' in your json data plaats = str(json_object['name'])

mtkilic
  • 1,213
  • 1
  • 12
  • 28
0

In your above request you were trying to access the list with out of the bound index i.e. [1], instead you have to use [0]:

def temperatuur():
    zipcode = '10024'
    r = requests.get('http://api.openweathermap.org/data/2.5/weather?zip='+zipcode+'&APPID=84c7d83bae2f2396ebd3a4a48dfdd057')
    json_object = r.json()
    weer = json_object['weather'][0]
    temp_k = int(json_object['main']['temp'])
    temp_c = (temp_k - 273)
    plaats = str(json_object['name'])
    return render_template('temperatuur.html', temperatuur=temp_c, plaats = plaats, weer = weer)

I believe you will get the desired result.