0
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))
response.raise_for_status()
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?

3
  • Your request is returning a JSON list, not a dictionary. Commented Jun 20, 2016 at 19:42
  • 1
    Can confirm that JSON returned by api.simplyrets.com/properties is array of objects at top level. Commented Jun 20, 2016 at 19:45
  • It's an array, since it starts with [{"privateRe and ends with est laborum."}] Commented Jun 20, 2016 at 19:50

5 Answers 5

1

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']
Sign up to request clarification or add additional context in comments.

Comments

0

From https://docs.python.org/2/library/json.html :

json.loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])

Deserialize s (a str or unicode instance containing a JSON document) to a Python object using this conversion table.

If s is a str instance and is encoded with an ASCII based encoding other than UTF-8 (e.g. latin-1), then an appropriate encoding name must be specified. Encodings that are not ASCII based (such as UCS-2) are not allowed and should be decoded to unicode first.

The other arguments have the same meaning as in load().

If your JSON starts as an array at the outermost layer, it will be an array. If your JSON's outermost layer is an associative array, then please post your JSON and we can look into it a little further.

Comments

0

The problem is that json.loads() doesn't necessarily return a dictionary. If the outside container of the JSON is a list, then json.loads() will return a list, where the elements could be lists or dictionaries. Try iterating through the list returned by json.loads(). It's possible the dictionary you're looking for is simply json.loads()[0] or some other element.

Comments

0

There are 2 different types of JSON elements: nodes and arrays.

A node looks like:

node = {
    foo = 7
    bar = "Hello World!"
}

A array looks like this:

array = [ "one", "two", 3, 4, "5ive" ]

Your JSON element is probably a array. You can verify whether it's an array, dict, or other by using:

isinstance(json_element, dict)
isinstance(json_element, list)

Hope this helps!

Comments

0

There are some minor changes you should do:

  1. Your API response is returning a list, so you have to iterate over it.
  2. The requests library already supports converting to JSON so you don't have to worry about it.

    import requests
    
    
    # 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))
    response.raise_for_status()
    houseData = response.json()
    
    # different parameters we need to know
    for data in houseData:
        p = data['property']
        roof = p["roof"]
        cooling = p["cooling"]
        style = p["style"]
        area = p["area"]
        bathsFull = p["bathsFull"]
        bathsHalf = p["bathsHalf"]
    

If you want to make sure you will have only one result, do an if statement to check this.

if len(houseData) != 1:
    raise ValueError("Expecting only 1 houseData.")

data = houseData[0]
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.