Using C# .NET to retrieve business data from Yelp

Yelp is a little different than Yellow Pages or Yahoo Local. For starters they only return data in JSON format. We’ll handle this and its other requirements with two libraries: SimpleOAuth.NET and JSON.NET. Both packages can be installed via NuGet.

Setup: Get your api keys

Start by registering with Yelp to get your api keys we’ll use to retrieve the json data.

You’ll need the following four keys:

  • Consumer Key
  • Consumer Secret
  • Access Token
  • Access Token Secret

Once you have your keys, the next step is to install the SimpleOAuth.NET and JSON.NET packages linked earlier. The easiest way is to use NuGet in Visual Studio. Once both are installed, the last thing to do is to get your web request url. For this post I’m going to use the following:

Retrieve the JSON data and parsing the contents

The container class:

internal class YelpResult
	internal bool IsClaimed { get; set; }
	internal decimal Rating { get; set; }
	internal int ReviewCount { get; set; }
	internal string Name { get; set; }
	internal string Url { get; set; }
	internal string Phone { get; set; }
	internal IEnumerable<string> Categories { get; set; }
	internal string DisplayPhone { get; set; }
	internal bool IsClosed { get; set; }
	internal string City { get; set; }
	internal string Address { get; set; }
	internal string Zip { get; set; }
	internal string Country { get; set; }
	internal decimal Latitude { get; set; }
	internal decimal Longitude { get; set; }
	internal string State { get; set; }

The code:

internal YelpResult GetYelpReviews(string businessId)
	string url = string.Format("{0}", businessId);
	var uriBuilder = new UriBuilder(url);
	const string consumerKey = ""; // Your consumer key
	const string consumerSecret = ""; // Your consumer secret
	const string token = ""; // Your token
	const string tokenSecret = ""; // Your token secret

	var request = WebRequest.Create(uriBuilder.ToString());
	request.Method = "GET";
		new Tokens
			ConsumerKey = consumerKey,
			ConsumerSecret = consumerSecret,
			AccessToken = token,
			AccessTokenSecret = tokenSecret

	JObject result;

	using (var response = (HttpWebResponse)request.GetResponse()) 
		using (var stream = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
			result = JObject.Parse(stream.ReadToEnd());

	var location = result.Value<JObject>("location");
	var coordinate = location.Value<JObject>("coordinate");
	var address = location.Value<JArray>("address").Children().Select(jt => jt.Value<string>());

	return new YelpResult
		IsClaimed = result.Value<bool>("is_claimed"),
		Rating = result.Value<decimal>("rating"),
		ReviewCount = result.Value<int>("review_count"),
		Name = result.Value<string>("name"),
		Url = result.Value<string>("url"),
		Phone = result.Value<string>("phone"),
		Categories = result.Value<JArray>("categories").Values<JArray>().Children().Select(jt => jt.Value<string>()),
		DisplayPhone = result.Value<string>("display_phone"),
		IsClosed = result.Value<bool>("is_closed"),
		City = location.Value<string>("city"),
		Address = string.Join(" ", address),
		Zip = location.Value<string>("postal_code"),
		Country = location.Value<string>("country_code"),
		Latitude = coordinate.Value<Decimal>("latitude"),
		Longitude = coordinate.Value<Decimal>("longitude"),
		State = location.Value<string>("state_code")

So here we’re using JSON.NET to parse the data we’ve retrieved by making a web request with SimpleOAuth.NET. First we sign the request with our tokens, make our request, and return our data into a JObject. From there, using JSON.NET we parse each value and return the result.

Wrapping Up

So hopefully this has been informative, if you’ve enjoyed reading please leave a comment below or see if I’ve made any mistakes or a better way of doing this I would love to hear it, thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *