2 years ago

#30751

test-img

OML

JSONDecodeError when data source changed to csv file

I'm new to Python so this may be a simple question. I'm modifying API code for Zillow so that I can list multiple cities at once. At first, the cities/states were directly inputted in the code (below), and that worked without problems.

d = {'city': ['Detroit', 'Tampa', 'Newark'], 
     'state': ['MI', 'FL', 'NJ'],
     'maximum_price': ['130000', '250000', '400000'],
     'minimum_price': ['50000', '50000', '50000']
     }
df = pd.DataFrame(data=d)
df

I'm changing that so that the cities are added to a csv, which the code reads instead (below). However, a json code later on in the API stopped working as a result, and I don't know why.

city_list = pd.read_csv(file_dir + 'List_of_Cities.csv')
df_cities = pd.DataFrame(data = city_list)
print(df_cities)

This is the code where I'm getting the error (has comment):

url = "https://zillow-com1.p.rapidapi.com/propertyExtendedSearch"

querystring = {"location": search_str,
                "home_type": "Houses",
                "minPrice": min_price, 
                "maxPrice": max_price, 
                "sqftMin": "1000"}

headers = {
      'x-rapidapi-host': "zillow-com1.p.rapidapi.com",
      'x-rapidapi-key': rapid_api_key
      }

z_for_sale_resp = requests.request("GET", url, headers=headers, params=querystring)

# transform to json
***z_for_sale_resp_json = z_for_sale_resp.json()*** #This code returns the error
# one list of all search results
api_response_list.append(z_for_sale_resp_json)

This is the error response:

/usr/lib/python3.7/json/decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Why is this error suddenly appearing and how can I fix it?

Edit: I included an image of the CSV. The CSV contains a list of cities, their states, and the min and max prices for each. The code then does a for loop so that all cities can be incorporated into the querystring.

https://i.stack.imgur.com/aZv8N.png

python

json

api

csv

jsondecoder

0 Answers

Your Answer

Accepted video resources