Yahoo’s YQL stand for Yahoo Query Language. It’s a query language very similar to SQL that lets users query their multiple web services using a single unified language so that users wouldn’t have to learn multiple APIs. The information about the service is here.
The YQL Console
The YQL console gives you examples of the statements that are used to query Yahoo’s APIs. These queries are what we will use to send as a parameter to the YQL service. For instance, in the screen shot below we have a query making a request to Yahoo’s Local service and searching for fast food locations.
Also note, there are other options that are included. We have the option of whether we want the information returned as XML or JSON. We can also choose if we want diagnostic and debug information returned in our result set. These option will function as the query-string parameters that we will send as part of our request to the YQL service.
- q : the YQL query HTML encoded
- format : this determines how we want our results(XML or JSON). The default is XML.
- diagnostics: a true or false value that lets us select if we want our results to come back with diagnostic information from Yahoo
Take some time to play with the console and create the statements that you will like to send to the service.
This service is available to the public, so you don’t have to create an API key if you don’t want. Yet, there are some restrictions that may be applied to the times you can call it they make be affected if you are making an anonymous call.
Let’s Get Started
To get started first we’ll open up Visual studio and create a console project called YQLExample.
First we need to add a few things to get this working. First add a reference to the System.Web assembly. This will give us access to the HttpUtility class that will allow us to encode the query we will send.
Next use the package console manager to get the Newtonsoft JSON serielizer using this command
PM> Install-Package Newtonsoft.Json
Now add the following using statement to the file.
The System.Net namespace will give us access to classes that will allow us to make the request to the web service and get results back. The Newtonsoft.Json.Linq namespace will give us access to classes that will help us to manipulate the returned JSON we will get back from the web service.
First build the string which will be used as the URL to the web service.
Take note of the format parameter we are adding that will make the service give us our results as JSON and the diagnostics parameter that will keep the service from returning parameter information back to us.
Next we will use the WebClient class to make a call to the service and to return our results back as a string.
Finally we use the Newtonsoft JSON serializer that gives us access to powerful classes that let us manipulate our results from the web service call.
First, we turn the results from the web call to a JObject object using the Parse function from the JObject class. Then we “travel” down the hierarchy of the JObject to cast our results as a JArray. this lets us have our results as an enumerable collection of JToken objects. We then loop through our collection and display the results to the console.
And that’s pretty much it. You can modify the YQL query that we send to the service as you like. I also advise playing with the console and checking out the YQL documentation. You will be amazed at the amount of information that is freely available.
Link to a “Gist” of the code in it’s entirety