Home > Uncategorized > Access #UK government #database of food establishments

Access #UK government #database of food establishments

share-default

If you’re developing an app or software that lists places to eat in the UK, it’s great to have a definitive database of every official food vending establishment in the UK, and Food.gov.uk provides this. The API is free, and doesn’t even require authentication. Here’s a sample

https://ratings.food.gov.uk/search-address/belfast/rating/1/100/json

Here’s the official docs

http://ratings.food.gov.uk/open-data-resources/documents/api-guidance.pdf

And here’s some NodeJS code that I wrote to parse this; (It’s taken from an Alexa app)

module.exports.List = function(city, callback) {

var response = “”;
var strUrl = “https://ratings.food.gov.uk/search-address/” + city + “/rating/1/100/json”;
request(strUrl, function (error, response, body) {
console.log(‘error:’, error); // Print the error if one occurred
console.log(body);
if (error == null)
{
var jBody = JSON.parse(body);
if (typeof jBody.FHRSEstablishment.EstablishmentCollection == “undefined”)
{
response = text.dyn().RESTAURANT_NONE
.replace(“{city}”, city );
callback(response);
return;
}
// Filter
var restaurants = jBody.FHRSEstablishment.EstablishmentCollection.EstablishmentDetail.filter(function(el){
return ( (el.PostCode || “”).indexOf(“BT”) == 0
&& el.BusinessTypeID == 1
&& el.AddressLine3.toLowerCase() == city.toLowerCase()
);
});
if (restaurants.length == 0)
{
response = text.dyn().RESTAURANT_NONE
.replace(“{city}”, city );
callback(response);
return;
}
var restaurantList = [];
restaurants.forEach(function(element){
restaurantList.push(element.BusinessName);
});
var strRestaurantList = customJoin(restaurantList, ” , “, ” ” + text.dyn().AND + ” ” );

response = text.dyn().RESTAURANT_LIST
.replace(“{city}”, city )
.replace(“{restaurantList}”, strRestaurantList );
callback(response);
}
});
};

function customJoin(arr,s1,s2){
return(arr.slice(0,-1).join(s1).concat(arr.length > 1 ? s2 : ”, arr.slice(-1)));
}

Hope this is useful to somebody!

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: