Talend Tutorials

Using tRest And Json Components To Access Api Data

Using tRest And Json Components To Access Api Data

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”

image 1

Step 3: Design your job

Now, create a tREST component on your canvas.

image 2

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

image 3

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

image 4

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)

image 5

Step 7: Save schema information

We want to save this schema, so in the component view, click the “…” button next to “Edit schema”

image 6

From here, click the “View Schema” option, and then save the schema to be used later.

image 7

Step 8: Load JSON data from file

To read this file, we now want a tFileInputJSON component to read the file we saved.

image 8

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.

image 9

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.

image 10

After doing this, the mapping area should be filled in, though with blank JSONPath queries.

image 11

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”.

image 12

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 this website we can find how to work the JSONPath query, and find the way to use these queries. A description table follows:

image 13

We can use the last query to retrieve all elements, so we’ll copy that into our component.

image 14

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.

image 15

Switch to the run tab and click the “Run” button to see the fruits of your labor!

image 16

Your output should return the JSON document from the API:

image 17

Looking For More Talend Resources and Help?
Ask An Expert