This topic describes how to use a filter to process the result data returned by APIs.

What is a filter?

In DataService Studio, a filter is a function used to process the result data returned by APIs. By using one or more filters, you can customize the structure of the result data returned by APIs and process the result data.
  • Currently, only Python 3.0 functions can be used as filters.
  • Filters only support importing the following modules: json, time, random, pickle, re, and math.
  • The function name of a filter must be def handler(event,context):.

Function template

The built-in function template is as follows:
# -*- coding: utf-8 -*-

# event (str) : in filter it is the API result, in other cases, it is your param
# context : some environment information, temporarily useless
# import module limit: json,time,random,pickle,re,math
# do not modify function name
import json
def handler(event,context):
# load str to json object
obj = json.loads(event) # Convert the string specified by the event parameter to a JSON object.
# add your code here
# end add
return obj
You can modify the function template to write your own function. You can modify the names of the input parameters as needed.
Parameter 1 [context]: the context of calling APIs. The value is of the String type. Currently, the parameter is empty and is not in use.  
Parameter 2 [event]: the result data returned by APIs or the preceding filter. The value is of the String type.

Create a filter

Note You must purchase DataWorks Professional Edition or higher editions before you create Python functions.
  1. Log on to the DataWorks console. In the left-side navigation pane, click Workspaces. On the Workspaces page, find the target workspace and click Data Analytics in the Actions column.
  2. On the DataStudio page, click the DataWorks icon in the upper-left corner and choose All Products > DataService Studio.
  3. On the left-side navigation submenu of the DataService Studio page, click the Function icon.
  4. Move the pointer over Create icon and select Create Python Function.
  5. In the Create Python Function dialog box that appears, set relevant parameters.
    Parameter Description
    Function Name The name of the function to create. The name can be up to 64 characters in length.
    Function Template The template used to create the function. Currently, only the template named Python3 Standard v1 is supported.
    Running Environment The runtime environment of the function. Currently, only Python 3.0 is supported.
    Function Description The description of the function.
    Target Folder The folder where you create the function.
  6. After the parameters are set, click OK.

Use a filter

  1. On the left-side navigation submenu, click the Service Development icon.
  2. Click the Service List folder and double-click the target API. In the right-side navigation pane, click the Response Parameters tab.
  3. Select Filter and select the created function from the drop-down list.
    Note You can create a function on the Function page and use it as a filter to process the query results returned by the API.
  4. Click Preview Response to view the processing results of the filter.