Parsing JSON in Ruby


A lot of APIs will give you responses in JSON format. Here we'll review JSON parsing in Ruby so that you can get to the interesting data faster.

This tutorial assumes that you've already gone through our Ruby getting started tutorial and are familiar with how our Ruby SDK works. 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 Ruby.

Get JSON output

1 Log in to Temboo and go to the YouTube > Search > ListSearchResults Choreo in our Library. Select Ruby from the drop down menu at the top of the page.

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 search. Next we'll see how to parse through this response in Ruby and pick out only the pieces we're interested in.

Parse it in Ruby

4 Create a new .rb file and copy in the code below. In steps 5-8, we'll go over what happens in the code.

require "json"

require "temboo"
require "Library/YouTube"

class JSONExample
    def initialize()
        @session = TembooSession.new("ACCOUNT_NAME", "APP_NAME", "APP_KEY")
    end

    def parse_json()
        search_choreo = YouTube::Search::ListSearchResults.new(@session)

        # Get an InputSet object for the choreo
        search_inputs = search_choreo.new_input_set()

        # Set inputs
        search_inputs.set_Query("quantum entanglement")

        # Execute Choreo
        search_results = search_choreo.execute(search_inputs)

        data = JSON.parse(search_results.get_Response())

        title = data["items"][0]["snippet"]["title"]
        description = data["items"][0]["snippet"]["description"]

        printf("The description for %s is: %s\n", title, description)
    end
end

instance = JSONExample.new()
instance.parse_json()

5First we converted the JSON text from the response to a list.

6 Next, we parsed 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:

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

For these results, we wanted the title and description of the first video in the search results. To do this, you'll use the following square bracket syntax for specifying the items array, then the first item in that array (at index 0), and finally the snippet object within the first item in the array:

LocationDescription
data["items"]The array of videos in the search results
data["items"][0]The first video within those results
data["items"][0]["snippet"]The snippet containing descriptive details of each video

7 To finish up, the title and description properties, nested within the snippet object, are assigned to local variables.

title = data["items"][0]["snippet"]["title"]
description = data["items"][0]["snippet"]["description"]

8All finished! Run the code to try it out. You should see the title of your first YouTube Search result in the console.

What next?

You should to able to parse all sorts of JSON responses with our Ruby SDK now. Why not flex your new parsing skills on all the JSON returned by the 2000+ Choreos in our Library?

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?

We're always happy to help. 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