In this post, we are going to walk you through the basic steps required to get started on accessing API information with the tREST component. This allows for simple extraction of data using any API interface. In this specific example, we will pull JSON data about a Spotify track search.
Step 1: Open Talend
Open Talend and create or open an existing project
Step 2: Create a new job
• Right click on Job Designs in the Repository window and select “Create job”
• Name the job “trest_extract”
Step 3: Design your job
Now, create a tREST component on your canvas.
Step 4: Find your API information
View the API information you’d like to use, in this case it is the Spotify Web API from mashape.com
From here we can see the URL and header information to access the API method we’d like to use
Alter the URL to the artist you’d like to see track information about (in this instance, we chose Lorde)
Step 5: Enter in API information
Click the green “plus” icon to add Header information, and copy in the header pair from the API method
Step 6: Create JSON components
Now that we can access the API, we need to save the JSON data it will retrieve. For this use a tFileOutputJSON component (if we were working with Talend Big Data, tExtractJSONFields would be a better choice)
Step 7: Save schema information
We want to save this schema, so in the component view, click the “…” button next to “Edit schema”
From here, click the “View Schema” option, and then save the schema to be used later.
Step 8: Load JSON data from file
To read this file, we now want a tFileInputJSON component to read the file we saved.
In the component tab, switch the schema type to “Repository”, unclick the “Read By XPath” checkbox, and edit the filename to match your tFileOutputJSON component.
Select a schema by clicking the “…” button next to the blank schema name to select the schema to use. Then navigate to your saved metadata information and select it.
After doing this, the mapping area should be filled in, though with blank JSONPath queries.
Since we don’t want to pull the Error Code data, you can also edit the metadata you just selected, and unclick the “Used Column” checkbox for “ERROR_CODE”.
Step 9: Choose JSON Path
Now we must execute a JSON query to retrieve the information we want, but for the purposes of this demo, we’ll just retrieve the whole document. From the website http://goessner.net/articles/JsonPath/ we can find how to work the JSONPath query, and find the way to use these queries. A description table follows:
We can use the last query to retrieve all elements, so we’ll copy that into our component.
Step 9: Run Job and View Output
Lastly, we want to insert a tlogrow component and be sure our two subjobs are connected with both main rows and a OnSubjobOk trigger to determine the order in which they run.
Switch to the run tab and click the “Run” button to see the fruits of your labor!
Your output should return the JSON document from the API: