Click here to Skip to main content
15,860,943 members
Articles / Programming Languages / C#

Yahoo’s YQL API and C# Tutorial

Rate me:
Please Sign up or sign in to vote.
4.71/5 (4 votes)
25 Feb 2013CPOL3 min read 28.5K   8   2
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.

Yahoo’s YQL

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.

YQL Query

YQL Query searching Yahoo’s Local service 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 options 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 may 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.

NewProject

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 serializer using this command:

PM> Install-Package Newtonsoft.Json

Now, add the following using statement to the file:

C#
using System.Net;
using System.Web;
using Newtonsoft.Json.Linq;

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.

BuildAddress

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.

GetStuffFromWeb

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.

Enumerate

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.

ConsoleResults

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 its entirety:

Related Links

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
Software developer in Nashville, TN(USA).

Comments and Discussions

 
QuestionRemote server error Pin
Member 1002797630-Sep-13 23:59
Member 1002797630-Sep-13 23:59 
AnswerRe: Remote server error Pin
SleepyCrat8-Oct-13 5:41
SleepyCrat8-Oct-13 5:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.