A lot of APIs will give you responses in JSON format. Here we'll review JSON parsing in Python so that you can get to the interesting data faster.
This tutorial assumes that you've already gone through our Python getting started tutorial and are familiar with how our Python 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 Python.
1 Log in to Temboo and go to the YouTube > Search > ListSearchResults Choreo in our Library. Select Python 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 Python and pick out only the pieces we're interested in.
4 Create a new .py file and copy in the code below. In steps 5-8, we'll go over what happens in the code.
from temboo.core.session import TembooSession from temboo.Library.YouTube.Search import ListSearchResults import json # Create a new Temboo session session = TembooSession('ACCOUNT_NAME', 'APP_NAME', 'APP_KEY') # Instantiate the choreo, using the session object listSearchResultsChoreo = ListSearchResults(session) # Get an InputSet object for the choreo listSearchResultsInputs = listSearchResultsChoreo.new_input_set() # Set inputs listSearchResultsInputs.set_Query("quantum entanglement") # Execute choreo listSearchResultsResults = listSearchResultsChoreo.execute_with_results(listSearchResultsInputs) # Convert JSON to a list data = json.loads(listSearchResultsResults.get_Response()) # Parse title and description properties. title = data["items"][0]["snippet"]["title"] description = data["items"][0]["snippet"]["description"] # Print title and description print ("Title: " + title) print ("Description: " + description)
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, we assigned the title and description properties that are nested within the snippet object 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.
Now you should to able to parse all sorts of JSON responses with our Python SDK. You can find lots of JSON responses to flex your parsing skills on 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.
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.