JSON and Processing API Tutorial

Using Processing and JSON to pull data from an API.

This is a tutorial for making a simple API call using Processing and JSON.

For our example, we are using the Twitter API.

    First Stop: API Documentation Page. There are several different types of API methods that we can make. To make sure that we are trying the right type of call, we should check the Parameters and the URL.

    The Parameters tell us what information is mandatory in making a call.

    For this example, we are going to pull our name, followers and the number of status updates we have using the users_show API method.

    Our Parameters are:


Let’s use screen_name for our Parameter. Next, you can check the variable names that we’re using for our API call under the XML example.

Name = name

followers= followers_count

status updates= status_count

Remember these variable names for later!

Setting Up Processing

In your Processing window, be sure to import org.json.*; The JSON library comes with Processing 1.0.6. If you have an older version, you can download the JSON library from the Processing.org website.

Now that we have our Library, we need some Strings for our API method:

Our URL for the call is: “http://twitter.com/users/show.json?

Our Parameter is: “screen_name”

Combined, it should read as: “http://twitter.com/users/show.json?screen_name=

We can place that in one String:

String baseURL=”http://twitter.com/users/show.json?screen_name=”;

The Username that we are using can be any valid username for twitter. In this example, we are using “dougw.” You can place this in a separate variable or attach it to the baseURL.

String userName=”dougw”;

Your call to the API is the combination of the URL and the Parameter.

String request= baseURL+userName;

Next we need to set up some global strings so that we can manipulate our data once we pull it. We need a string for each variable we are pulling. NOTE: The number of Followers is actually an integer, so be sure to remember to correctly declare it!

String myName, numUpdate;

int numFollowed;

Your Processing Window should look like this:


Try and Catch a JSON Object

Now that you have a basic set up, you need the actual call.

If successful, your JSONObject twCount will contain all of the information that was listed on the API Documentation page.

try{

JSONObject twCount= new JSONObject(join(loadStrings(request),” “));

println(“Success Twitter API”);

}

catch(JSONException e){

println(“Uh Oh! Exception found in trying to get the JSONObject”+” “+e);

}

All of your API calls using Processing and JSON will have a try and catch condition. Everything else in this tutorial will be placed after the println(“Success Twitter API”);

NOTE: If you are unsuccessful with your API call, you will receive a JSONException, which you cross reference on the API Documentation Page.

Remember those variable names for the API method, that we looked up earlier? We are going to connect our global variables to the JSONObject and the variable names from the API method.

followers=twCount.getInt(“followers_count”);

user=twCount.getString(“name”);

posts=twCount.getString(“statuses_count”);

Add some println(); to test that your request is correct and that you are receiving all of the data that you are trying to pull.

Your Processing Window should now look like this:


And your Results:

Leave a Reply

Your email address will not be published.