A lot of APIs will give you responses in JSON format. Here we'll review JSON parsing in Java so that you can get to the interesting data faster.
This tutorial assumes that you've already gone through our Java getting started tutorial and are familiar with how our Java 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 Java.
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 Java and pick out only the pieces we're interested in.
4 Create a new Java class and copy in the code below. In steps 5-8, we'll go over what happens in the code.
import org.json.JSONArray; import org.json.JSONObject; import com.temboo.Library.YouTube.Search.ListSearchResults; import com.temboo.Library.YouTube.Search.ListSearchResults.ListSearchResultsInputSet; import com.temboo.Library.YouTube.Search.ListSearchResults.ListSearchResultsResultSet; import com.temboo.core.TembooSession; public class SearchVideos { public static void main(String[] args) throws Exception { // Create a new Temboo session. TembooSession session = new TembooSession("ACCOUNT_NAME", "APP_NAME", "APP_KEY"); // Instantiate the YouTube.Search.ListSearchResults Choreo, using the session object. ListSearchResults listSearchResultsChoreo = new ListSearchResults(session); // Get an InputSet object ListSearchResultsInputSet listSearchResultsInputs = listSearchResultsChoreo.newInputSet(); // Set inputs listSearchResultsInputs.set_Query("quantum entanglement"); // Execute choreo ListSearchResultsResultSet listSearchResultsResults = listSearchResultsChoreo.execute(listSearchResultsInputs); // Parse JSON response using org.json JSONObject results = new JSONObject(listSearchResultsResults.get_Response()); // Get items array JSONArray items = results.getJSONArray("items"); // Get first item JSONObject item = items.getJSONObject(0); // Get the snippet object within the first item JSONObject snippet = item.getJSONObject("snippet"); // Parse the title and description fields String title = snippet.getString("title"); String description = snippet.getString("description"); // Print title and description System.out.println(title); System.out.println(description); } }
5First we converted the JSON text from the response to a Java Object using a JSONObject
constructor.
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 of the first video in the search results. We got the items array, then the first item in that array (at index 0). Then we wanted the snippet object within the first item in the array. We used the getJSONArray
and getJSONObject
methods for this. In other words:
Method name | Description |
---|---|
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, we parsed the title property from the snippet object using the getString
method.
8All finished! Run the code in your Java IDE to try it out. You should see the title of your first YouTube Search result in the console.
Now you should be to able to parse all sorts of JSON responses with our Java SDK. Check out the 2000+ Choreos in our Library and find some exciting data to parse.
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.