R12.1-2025Jul03

API Parser in Visual Parser

An API Parser defines how to retrieve and parse data from a third-party system via API. You need to learn what data is required to be pulled from the third-party system and the corresponding API to query the data. Before setting up an API parser, you need to prepare the required Scripts to retrieve the API result, then use the interactive interface to finish the parser definition.

To achieve this, NetBrain supports the following:

  • Support Traditional Device/Logic Node in Parser Qualification
  • Support API Data Type
  • Support defining Retrieve Data Function
  • Support parsing JSON result

The following screenshot shows a defined API parser.

To define an API parser, follow the steps below. 
  1. Click the  icon on the taskbar and select New Parser.

  2. In the Select Data list, select API.
  3. In the API Adapter field, select Devices of Cisco ACI

  • Click Function, then define the functions to retrieve raw data by inputting or copying-and-pasting scripts. In this case, the system pulls parameters such as interface name, MTU data. 

    A sample script is given below:

    JSON
    '''
    Begin Declare Input Parameters
    [
    ]
    End Declare
    '''

    def BuildParameters(context, device_name, params):
    get_dn = GetDeviceProperties(context, device_name, {'techName': 'Cisco ACI', 'paramType': 'SDN', 'params' : ['dn','role'] })
    dn = get_dn['params']['dn']
    role = get_dn['params']['role']
    rtn_params = [{ 'devName' : device_name, 'dn' : dn, 'role': role }]
    return rtn_params

    def RetrieveData(rtn_params):
    dn = rtn_params['dn']
    #raise(Exception(str(dn)))
    role = rtn_params['role']
    url = '/api/class/cnwPhysIf.json'
    rtn_params['url'] = url
    data = getData(rtn_params)
    #return data
    temp = json.loads(data)
    json_object = {str(i): obj for i, obj in enumerate(temp)}
    json_string = json.dumps(json_object,indent=2)
    return json_string

    The script consists of three major parts:

    Retrieve parameters (optional): Manually input parameters required for data retrieval. The parameter values will be converted to parameters for the Build Parameters Function.

    Build Parameters function: Build the parameter objects used by the Retrieve Function. The values of the Build Parameters Function are device objects.

    RetrieveData function: This function primarily serves for defining the scripts for retrieving sample data. The scripts will be submitted to the API server to be run.

  1. Click Select Device, then select the device that the API parser applies to. Devices can be filtered by Device Type/Device Group/Site. You can search for the required device.
  2. Retrieve data: Click Live Data and select the data source for retrieving data (Live Data is the default option), then click Retrieve. The API sample data will be added to the sample text input pane. 

    In the retrieving process, the system will verify the following:
    a. The retrieved text is in complete and standard format (for example, standard Json format).
    b. The "name" parameter exists.
    c. Each parameter has its unique name. There is no parameter name duplication.
    Alternatively, you can also manually input the sample text or edit the retrieved sample text.

  1. Define line patterns for parsing variables (for the steps of defining the line pattern, refer to the steps in Define a Variable Parser/Define a Paragraph Parser/Define a Table Parser), then click Apply to apply the line patterns. You can check the parsed variable data in the Output pane.

  2. Click the save icon  to save the parser.