Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

Parse White Pages API using JQuery

4.50/5 (2 votes)
16 Mar 2010CPOL1 min read 1   113  
Basic example of the White Pages People and Reverse Phone Searches

Introduction

Searched for some examples on the White Pages Json API, and haven't found much. If you look at their documentation, http://developer.whitepages.com/docs, you'll notice a pretty good explanation of everything, but it does take a little time to parse everything out. I turned to the YQL Console in order to test and figure out how everything was broken down and developed a script to have a basic search of People by Name and Zip Code, and a Reverse Phone Number Search in order to tell what number belongs to who.

For further information, I am using YQL, (documentation is found at: http://developer.yahoo.com/yql/), and the White Pages API, (documentation is found at: http://developer.whitepages.com).

Using the Code

If you download the source, it will not work until you have signed up with Whitepages.com first in order to get an API Key. Once you have this, simply plug it into the 2 javascript files located under the Scripts/whitepages folder at the top where it says:

JavaScript
var apiKey = 'YOUR API KEY';

The scripts themselves are pretty lengthy, so I'll keep the summary fairly short...

JavaScript
function findPerson(name,zip) {   
$('#data').empty();
 var apiKey = 'YOUR API KEY';
var query = 'select * from json where url=" + name + 
	';zip=' + zip + ';api_key=' + apiKey + ';outputtype=JSON"'
  $.getJSON(
  "http://query.yahooapis.com/v1/public/yql?q=" + encodeURIComponent(query) + 
	"&format=json&callback=?",
  function(data) {
      $.each(data.query.results,
    function(index, item) {
      if (item.result.code == 'Found Data') {
    // Declare variables and display data
if (item.meta.recordrange.totalavailable == 1) {
//One item was found, parse accordingly
}
}
else{
// Multiple items are found, parse accordingly
}
else{
//No items were found
}}  
JavaScript
function findNumber(number) {   
$('#phoneData').empty();
 var apiKey = 'YOUR API KEY';
var query = 'select * from json where url=" + number + ';api_key=' + 
	apiKey + ';outputtype=JSON"';  
$.getJSON(
  "http://query.yahooapis.com/v1/public/yql?q=" + 
	encodeURIComponent(query) + "&format=json&callback=?",
  function(data) {
      $.each(data.query.results,
    function(index, item) {
      if (item.result.code == 'Found Data') {
    // Declare variables and display data
}
else{
//No items were found
}}   

Pretty much it.
View sample for further details.

A live example is located at http://www.joshuablackstone.net/WhitePagesAPI. (Forgive the Godaddy ads... lol).

Finally, I realize that there may be better ways to do this, and I am NOT an expert in JavaScript, so if you have a way to make this even better, then feel free to comment.

History

  • 16th March, 2010: Initial post

License

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