A lot of APIs will give you responses in JSON format. Here we'll review JSON parsing in Node.js so that you can get to the interesting data faster.
This tutorial assumes that you've already gone through our Node.js getting started tutorial and are familiar with how our Node.js 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 Node.js.
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 search. Next we'll see how to parse through this response in Node.js and pick out only the pieces we're interested in.
4 Create a new .js file and copy in the code below. In steps 5-8, we'll go over what happens in the code.
// You'll need a single TembooSession object in your code, eg: var tsession = require("temboo/core/temboosession"); var session = new tsession.TembooSession("ACCOUNT_NAME", "APP_NAME", "APP_KEY"); var YouTube = require("temboo/Library/YouTube/Search"); var listSearchResultsChoreo = new YouTube.ListSearchResults(session); // Instantiate and populate the input set for the choreo var listSearchResultsInputs = listSearchResultsChoreo.newInputSet(); // Set inputs listSearchResultsInputs.set_Query("quantum entanglement"); // Run the choreo, specifying success and error callback handlers listSearchResultsChoreo.execute( listSearchResultsInputs, function(results) { var parsedResponse = JSON.parse(results.get_Response()); var title = parsedResponse["items"][0]["snippet"]["title"]; var description = parsedResponse["items"][0]["snippet"]["description"]; console.log("The title is %s.", title); console.log("The description is %s", description); }, function(error) { console.log(error.type); console.log(error.message); } );
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 name | How 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:
Location | Description |
---|---|
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 variables.
var title = parsedResponse["items"][0]["snippet"]["title"]; var description = parsedResponse["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.
You should to able to parse all sorts of JSON responses with our Node.js SDK now. You can find lots of JSON to play with in our Library of over 2000+ Choreos.
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.
We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.
Like what we do? Take a look at our open positions.