jack_f jack_f - 2 years ago 77
JSON Question

How to put dowloaded JSON data into variables in python

import requests
import json
import csv

# These our are demo API keys, you can use them!
#location = ""
api_key = 'simplyrets'
api_secret = 'simplyrets'
#api_url = 'https://api.simplyrets.com/properties?q=%s&limit=1' % (location)
api_url = 'https://api.simplyrets.com/properties'

response = requests.get(api_url, auth=(api_key, api_secret))
houseData = json.loads(response.text)

#different parameters we need to know
p = houseData['property']
roof = p["roof"]
cooling = p["cooling"]
style = p["style"]
area = p["area"]
bathsFull = p["bathsFull"]
bathsHalf = p["bathsHalf"]

This is a snippet of the code that I am working with to try and take the information from the JSON provided by the API and put them into variables that I can actually use. I thought that when you loaded it with json.loads it would become a dictionary. Yet it is telling me that I cannot do p = houseData['property'] because "list indices must be integers, not str". Am I wrong that houseData should be a dictionary?
P.S I am new to python (as you can probably tell) so if it's probably something really stupid but I would love any help I can get as this has just been confounding me.

Answer Source

There are hundreds of properties returned, all of which are in a list.

You'll need to specify which property you want, so for the first one:

p = houseData[0]['property']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download