Parsing JSON in Processing


A lot of APIs will give you responses in JSON. Here we'll review JSON Parsing in Processing so that you can get to the interesting data faster. Although we use the output from our YouTube ListSearchResults Choreo in this tutorial, the same steps we outline here will work for parsing any JSON in Processing.

Get a JSON output

1 Log in to Temboo and go to the YouTube > Search > ListSearchResults Choreo in our Library.

2 Enter any search term you want for the Query input and click Generate Code to test the Choreo from our website.

3 You get a whole bunch of JSON in the Response output. These are the results of the YouTube search. Click the small COPY button, paste the JSON into an empty text file, and save as "response.json".

Parse it in Processing

4 Create a new Sketch in Processing and copy in the code below. Save the Sketch and place the "response.json" file that you created earlier into the same folder as your new Sketch. In steps 5-8, we'll go over what happens in the code.

// Declare some global variables
JSONObject response;
String videoTitle; 

void setup() {
  // Load the JSON file as a JSON object
  response = loadJSONObject("response.json");
  
  // Get the array of items from the JSON object
  JSONArray items = response.getJSONArray("items");
  
  // Get the first item in the JSON array
  JSONObject firstVideo = items.getJSONObject(0);
  
  // Get a JSON Object with a list of details about the first item
  JSONObject snippet = firstVideo.getJSONObject("snippet");
  
  // Get title the string from final JSON object
  videoTitle = snippet.getString("title");
  
  // Print the title of the first video result
  println(videoTitle);
}

5Now let's walk through what this Sketch actually does. First, we have to load the JSON data using loadJSONObject(fileName). The file name should be the same as the one you used in Step 3.

6 Next, we need to parse out the piece of data we want from the JSON. It helps to look at the JSON file's structure to get an idea of how it is organized. The two main elements you should look for are:

Element nameHow it appears in the JSON file
JSONArray"name": [
JSONObject"name": {

For these results, we want the title of the first video in the search results. First we need to load the JSON array of items, then the first JSON object (0) in that array. Finally, we want the snippet of details within that JSON object. In other words:

In codeDescription
getJSONArray("items")The array of videos in the search results
getJSONObject(0)The first video within those results
getJSONObject("snippet")The snippet containing descriptive details of each video

7 To finish up, you have to actually pull the content from the JSON object. For our example we'll use getString("title").
(Learn about other JSON get methods.)

8All Finished! Run the sketch to try it out. You should see the title of your first YouTube Search result.

What next?

You should be ready to tackle all sorts of JSON parsing now. Our Twitter example would be a great place to go next!

Once you've got your code up and running, you're ready to move on and do more. From monitoring your running applications, to moving your generated Temboo code to your preferred development environment and sharing it with colleagues, collaborators and friends - we've got you covered.

Need help?

Processing provides additional JSON method documentation. We're always happy to help too. Just email us at support@temboo.com and we'll answer your questions.

We're hiring!

Like what we do? Take a look at our open positions.


Back