String Handling

Extracting and manipulating strings in returned payloads

Key Concepts

Within APImetrics, it is possible to use advanced string handling functionality. to extract and manipulate strings in returned payloads.

String Handling in Returned Content

If you need to extract part of a returned value, you may take advantage of some filters that you can apply to a variable path using the the | operator.

VariableDescriptionParameters
intConvert value to an integerNA
lengthReturns the length of a stringNA
lowerConverts a value to lowercaseNA
reverseReverses a stringNA
round(precision=0,method='common')Round the number to a given precision.
The first parameter specifies the precision (default is 0), the second the rounding method.
If no method is specified 'common' is used
precision - numerical value
common - rounds either up for values of greater than or equal to 5 in the last decimal place of the number before rounding or down for values equal to less than 4
ceiling- always rounds up
floor - always rounds down
upperConverts a string to uppercase

Example: Extracting a URL

Assuming a JSON response:

{
    "url": "https://www.example.com/some/path/to/something"
}

If you wanted to extract the path and make it uppercase, in the Conditions tab you would use the following expression:

url|replace('https://www.example.com', '')|upper

This will give the following result:

/SOME/PATH/TO/SOMETHING

Example: Extract an Element from a URL and Set a Variable

📘

Scenario

The API returns a variable "/abc/def/ghi" and you want to extract the middle part.

You want to extract it as path_to_var.replace("abc/", "")|replace("/ghi", "") if those values were hard coded. This just removes the abc/ and /ghi parts.

To handle this programmaticly, use the expression:

path_to_var.split("/")[1]

or

path_to_var.split("/").1

Underlying this is a Python string object, which has a split function available that converts it to a list of sub-strings. It is then possibly to get the second item in the list (i.e. position 1).

741