Output filters for targeting data on your Arduino

We’ll show how Temboo helps you handle results from APIs when you run Choreos.

Many APIs return complex JSON or XML data that can be difficult to parse, particularly on devices with limited memory like your Arduino board. But with Temboo, you can select which pieces of JSON or XML data you’d like to have returned. The key is specifying one or more output filters when you call a Choreo. We'll demonstrate this with the same Yahoo Weather Choreo used in our Getting Started tutorial.

Filtering API Results

When you run the sketch from our getting started example, you see an XML response returned by Yahoo Weather printed to the Arduino serial monitor. Let's use output filters to extract just the date of the last weather report and the current temperature from this XML.

1Add the following lines of code to the Yahoo WeatherByAddress sketch, just before the call to the run() method:

// add an output filter to extract the current temperature
GetWeatherByAddressChoreo.addOutputFilter("temperature", "/rss/channel/item/yweather:condition/@temp", "Response");

// add an output filter to extract the date and time of the last report.
GetWeatherByAddressChoreo.addOutputFilter("date", "/rss/channel/item/yweather:condition/@date", "Response");

2Upload and run the sketch. Instead of seeing the XML returned by Yahoo Weather, you should now see individual values for "temperature" and "date" printed to the serial monitor in a format that can be easily read into individual variables for use in your sketch.

What Just Happened?

Output filters provide a powerful mechanism for reducing the complexity of API results. To understand how they work, we'll walk through the process of creating a new output filter from scratch.

As we saw above, each output filter is constructed with three parameters, in the format: Choreo.addOutputFilter(result_label, data_path, choreo_output).

Result label is the label with which you’d like Temboo to identify this piece of result information when it’s returned to the Arduino. Labels can contain letters and numbers, and you can choose any label you’d like for an output filter.

Data path is used to identify the particular item within a JSON or XML result that you’d like to have returned to the Arduino. The data path can either be an XPath statement, or a JSON path.

Choreo output is the name of the Choreo output that contains the JSON or XML data to be filtered.

Identifying the Data to Filter

To construct an output filter, you’ll need to create an XPath or JSON path statement that identifies the data within the API response you want returned.

For example, given the result XML:


To return only the value of the <temperature> element, your XPath statement would be:


If you’re not familiar with XPath, you can find a good general introduction here. There are also a number of useful online tools that can help with constructing XPath statements, like the this one.

The paths you use to specify JSON elements work much the same way (although they don’t support more advanced XPath features like attribute-value selectors). Given the JSON:

    “weather” : {
        “temperature”: “68”,
        “humidity”: “45”

to retrieve the “temperature” value, you’d specify a data path of:


In cases where you need to get a JSON or XML element at a particular array index, you can use the square bracket syntax to specify which element in the array you want. Array indices start at 1 (rather than 0). So to retrieve the first element in the /weather/temperature array, you'd use:


If you want to return all elements in a collection, you specify empty brackets, [], after the collection in question. So, to retrieve all elements in the /weather/temperature array, you'd use:


If you're targeting a collection that is at the top-level of the returned data, you can start your output filter specification with empty brackets e.g., /[]/var_name1/var_name2.

In this example, we'll define an additional output filter for the Yahoo Weather API to extract the forecast city from the response XML.

Assembling the Output Filter

When constructing an output filter, you’ll typically need to start with a sample of the JSON or XML returned by the Choreo. Running a Choreo from the Temboo Library is a convenient way to obtain this output.

To create the output filter, follow these steps:

1Run the Yahoo > Weather > WeatherByAddress Choreo using the Temboo Library website (using the same process explained in the first Getting Started tutorial). After the Choreo has run, you’ll see the XML returned in the Output section.

2Note the name of the Choreo output for which the XML data was returned. Some Choreos have more than one named output. In most cases, for Library Choreos, the relevant Choreo output name will be Response, like in the screenshot below.

Yahoo! Weather Response Output

A Choreo output named Response

3Identify the XPath or JSON path for the filtered information you want returned by Temboo.

The XML returned by the Yahoo > Weather > GetWeatherByAddress looks like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rss xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0">
    <title>Yahoo! Weather - Portland, OR</title>
    <description>Yahoo! Weather for Portland, OR</description>
    <lastBuildDate>Mon, 24 Jun 2013 11:53 am PDT</lastBuildDate>
    <yweather:location city="Portland" region="OR" country="United States"/>
    <yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
    <yweather:wind chill="60" direction="170" speed="5"/>
    <yweather:atmosphere humidity="80" visibility="10" pressure="29.72" rising="0"/>
    <yweather:astronomy sunrise="5:21 am" sunset="9:01 pm"/>
        <title>Yahoo! Weather</title>
        <title>Conditions for Portland, OR at 11:53 am PDT</title>
        <pubDate>Mon, 24 Jun 2013 11:53 am PDT</pubDate>
        <yweather:condition text="Light Rain" code="11" temp="60" date="Mon, 24 Jun 2013 11:53 am PDT"/>

(The full XML response has been shortened here for clarity)

We’ll want to retrieve the value of the city attribute within the <yweather:location> element. The XPath statement for this value will be:


That is, the city attribute (specified by the "@" symbol) within the XML element located at /rss/channel/yweather:location. Note that you could also specify this element using:


(The // means "regardless of the parent XML element’s location.")

4Finally, choose a label that Temboo will use to identify the filtered data when it’s returned to the Arduino. In this case, we’ll use city as the label.

We now have all the pieces to assemble the output filter:

Using Output Filters

After defining the components of your output filter, you can specify it as a parameter argument in your Arduino sketch using the addOutputFilter(label, path, output) method. For this output filter, the code will look like:

GetWeatherByAddressChoreo.addOutputFilter("city", "/rss/channel/yweather:location/@city", "Response");

The complete loop() method for your sketch should now look like:

* Imports and setup code go here. 
* This code will vary depending on which internet shield you're using

void loop()
  // while we haven't reached the max number of runs...
  if (numRuns <= maxRuns) {
    // print status
    Serial.println("Running GetWeatherByAddress - Run #" + String(numRuns++) + "...");

    // create a TembooChoreo object to send a Choreo request to Temboo
    TembooChoreo GetWeatherByAddressChoreo(client);
    // invoke the Temboo client

    // add your temboo account info
    // set the name of the choreo we want to run
    // set choreo inputs; in this case, the address for which to retrieve weather data
    // the Temboo client provides standardized calls to 100+ cloud APIs
    GetWeatherByAddressChoreo.addInput("Address", ADDRESS_FOR_FORECAST);

    // add an output filter to extract the name of the city.
    GetWeatherByAddressChoreo.addOutputFilter("city", "/rss/channel/yweather:location/@city", "Response");
    // add an output filter to extract the current temperature
    GetWeatherByAddressChoreo.addOutputFilter("temperature", "/rss/channel/item/yweather:condition/@temp", "Response");

    // add an output filter to extract the date and time of the last report.
    GetWeatherByAddressChoreo.addOutputFilter("date", "/rss/channel/item/yweather:condition/@date", "Response");

    // run the choreo 
    // when the choreo results are available, print them to the serial monitor
    while(GetWeatherByAddressChoreo.available()) {
      char c = GetWeatherByAddressChoreo.read();    

  delay(30000); // wait 30 seconds between GetWeatherByAddress calls

You can specify as many output filters as you need for each Choreo, although each filter must have a unique label.

Save and upload your sketch. You should now see individual outputs for "city," "temperature," and "date" printed to the serial monitor.

Need Help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.