Filter API Responses with Output Filters for the C# SDK


Temboo can help you reduce the complexity of API responses. Just specify one or more Output Filters when you call a Choreo.

What's an Output Filter?

It’s a filter that returns only the data that you care about!

Many APIs return a lot of complex JSON or XML data that can be difficult to parse. Let's take a look at how we can use Output Filters with the C# SDK to make API responses easier to work with.

Before You Begin

Make sure that you've been through the C# getting started tutorial. In the following steps, we'll build upon that tutorial by adding Output Filters.

1In the getting started tutorial, we printed out the Latitude. To setup our Output Filters, let's print out all of the returned data at once. Since this example deals with handling collections of data, you'll want to start by adding the following imports to your code:

using System.Collections;
using System.Collections.Generic;

Next, go ahead and replace the Latitude WriteLine statement from the getting started example with the following:

foreach (KeyValuePair<string, object> item in geocodeByAddressResults.Output)
{
    Console.WriteLine("the value of " + item.Key + " is:");
    Console.WriteLine(item.Value);
    Console.WriteLine("press any key to continue...");
    Console.ReadKey();
}

2Run the code and take a look at the console. See the XML response? There's a lot of data!

3What if the only information we need for our application is the formatted_address, and a collection of individual address_components that make up that address? Introducing Output Filters will allow us to extract only those data elements by adding just two lines of code. Add the following lines before the geocodeByAddressChoreo.execute(); method:

//add an output filter to extract the full address
geocodeByAddressChoreo.addOutputFilter("address", "/GeocodeResponse/result/formatted_address", "Response");
//add an output filter to extract the collection of address long_name components
geocodeByAddressChoreo.addOutputFilter("components", "/GeocodeResponse/result/address_component/long_name", "Response");

4Instead of printing out each item in this Output object like we did earlier, let's print out the new address output, and then iterate over the components output (which is a collection):

// Print address
Console.WriteLine(geocodeByAddressResults.Output["address"]);
Console.WriteLine("Press any key to continue...");
Console.ReadKey();

// Print each item in the components collection
foreach (Object obj in (ArrayList)geocodeByAddressResults.Output["components"])
    Console.WriteLine("{0}", obj);
    Console.WriteLine("Press any key to continue...");
    Console.ReadKey();

5Run the code again and take a look at the console. You should see the full address, as well as a collection of the long_name elements from the XML.

104 Franklin Street, New York, NY 1003, USA
Press any key to continue...
104
Franklin Street
Lower Manhattan
Manhattan
New York
New York County
New York
United States
10013
Press any key to continue...

How Did we do it?

Let's take a look at how the filter is created. As we saw above, each Output Filter is constructed with three parameters, in the format:

Choreo.addOutputFilter(result_label, data_path, choreo_output)

It may look confusing at first, but let’s step through recreating the two Output Filters from scratch to understand what's happening.

1A Choreo object is used to specify the inputs that will be passed to the Choreo on the Temboo platform. Output Filters are also a type of Choreo input, so we'll add our inputs to the Choreo object. To find the Choreo object that we need to use when adding the Geocoding Output Filters, just look back at the following line in your code:

// Set inputs
geocodeByAddressChoreo.setAddress("104 Franklin St., New York NY 10013");

After identifying the input that we'd like to filter, add .addOutputFilter as shown below.

geocodeByAddressChoreo.addOutputFilter(result_label, data_path, choreo_output);

2A result_label is anything we want it to be! It can be data123 or apple or Homer, as long as each Output Filter has a unique result_label of numbers or letters. This is how we tell Temboo to identify a piece of result information when it is returned. In our example, we called our collection of address elements “components”. We called the full address "address".

3The data_path is used to identify, via XPath or a JSON path, the location of the particular item(s) that you want your Output Filter to return. For a more in depth explanation of data paths and data types, check out our guide on JSON & XML for Output Filters. Here are the paths that we used when specifying our Output Filters earlier.

 /GeocodeResponse/result/address_component/long_name
 /GeocodeResponse/result/formatted_address

4The Choreo_output is the name of the Choreo output data that we're applying our output filter to. These names can be found in each Choreo's output section on our website. In most cases, the relevant Choreo output name will be Response, like in the screenshot below. However, as you can see we can also retrieve Latitude and Longitude in this particular Choreo.

Where to find the Choreo outputs returned by Temboo for a Choreo

5When we combine all of this information, we have two Output Filters. One returns only the full formatted address as a string, and another returns a collection of elements that make up the address as a list.

//add an output filter to extract the full address
geocodeByAddressChoreo.addOutputFilter("address", "/GeocodeResponse/result/formatted_address", "Response");
//add an output filter to extract the collection of address long_name components
geocodeByAddressChoreo.addOutputFilter("components", "/GeocodeResponse/result/address_component/long_name", "Response");

Here's the full code

The complete code takes advantage of two Output Filters. It should look like this:

using System;
using System.Collections;
using System.Collections.Generic;
using Temboo.Core;
using Temboo.Library.Google.Geocoding;

namespace TestProject
{
    class Program
    {
        static void Main(string[] args)
        {
            // Instantiate a TembooSession object using your Account name and Application key
            TembooSession session = new TembooSession("ACCOUNT_NAME", "APP_NAME", "APP_KEY");

            // Instantiate the Choreo using the TembooSession
            GeocodeByAddress geocodeByAddressChoreo = new GeocodeByAddress(session);

            // Set inputs
            geocodeByAddressChoreo.setAddress("104 Franklin St., New York NY 10013");

            // Add an output filter to extract the full address
            geocodeByAddressChoreo.addOutputFilter("address", "/GeocodeResponse/result/formatted_address", "Response");
            // Add an output filter to extract the collection of address long_name components
            geocodeByAddressChoreo.addOutputFilter("components", "/GeocodeResponse/result/address_component/long_name", "Response");

            // Execute Choreo
            GeocodeByAddressResultSet geocodeByAddressResults = geocodeByAddressChoreo.execute();

            // Print address
            Console.WriteLine(geocodeByAddressResults.Output["address"]);
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();

            // Print components collection
            foreach (Object obj in (ArrayList)geocodeByAddressResults.Output["components"])
                Console.WriteLine("{0}", obj);
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
        }
    }
}

You should now see the full "address" as a single string, as well as individual outputs for each of the address "components", printed to the console.

What's Next?

We've filtered some data! You can specify as many Output Filters as you need for each Choreo, so why not try adding your own third output filter to this example? If you'd like to try something different, you can try filtering the output from any of our 2000+ Choreos.

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.

Need Help?

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


Back