Good day everyone, I’ve fetched this set of data from new york city’s open data API portal and am trying to sort all entries by endpoint inspection_date within the past two years. I’d also like to sort the results by the latest times first. However, I’m having trouble getting my endpoints to cooperate. I’ve read the API documentation several times over and can’t figure out how it works as I’m still rather new to APIs. Would appreciate any pointers anyone can provide. Thanks!
url.search = new URLSearchParams({ "$$app_token": app.token, "$limit": 5, "house_number": house, "street_name": street, "borough": borough, "inspection_date": ???? });
Advertisement
Answer
From this part of the docs we can see query parameters that we can use.
As you are trying to sort all entries by inspection_date within the past two years, you can use $order
and $where
query parameter.
$order
:inspection_date
$where
:inspection_date between x and y
change x as the start date and y as the end date. In your case, the x is 2 years from y.
Example with x as ‘2020-10-08’ and y as ‘2022-10-08’: https://data.cityofnewyork.us/resource/p937-wjvj.json?$order=inspection_date&$where=inspection_date%20between%20%272020-10-08%27%20and%20%272022-10-08%27
to build the query you will add query parameter $where
and $order
:
url.search = new URLSearchParams({ "$$app_token": app.token, "$limit": 5, "$where":`inspection_date between ${startDate} and ${endDate}` "$order":"inspection_date", "house_number": house, "street_name": street, "borough": borough, });