Transforming your data - Cloud

Talend Cloud API Tester User Guide

Version
Cloud
Language
English
Product
Talend Cloud
Module
Talend API Tester
Content
Design and Development > Testing APIs
Last publication date
2024-03-27

Talend Cloud API Tester allows you to manipulate data via functions.

Functions can create new data or transform existing data. The functions that transform data apply on the value of the expression they finish.

Usage examples

If you want to retrieve the first 10 planet names in the list of planets in your Star Wars API, you can use the expression: ${"Star-wars API"."Get 10 planet names"."response"."body".jsonPath("$.results[:10].name")}. The method jsonPath parses the result of the expression ${"Star-wars API"."Get 10 planet names"."response"."body"}.

If you want to retrieve logs from the past week, you can create a URL with expressions in the query parameters: https://myapi.com/logs?from=${timestamp().addTime("WEEK", "-1")}&to=${timestamp()}.

If you want to generate the current date in ISO format, you can use the expression ${timestamp().formatDate('TIMESTAMP', 'ISO_8601')}.

Constants

When writing an expression like ${"toto"}, the expression is evaluated as:

  • The value of the current environment's variable toto if there is one.
  • The request/scenario/service/project toto that is at root-level in your repository if no environment variable matched.
  • The string toto in any other case.

Data creation functions

Function Description Arguments Example
timestamp Returns the number of milliseconds from January 1, 1970, 00:00:00 GMT to the date when it is evaluated. none Expression: ${timestamp()}

Possible result: 1505136142950

uuid Returns a UUID. none Expression: ${uuid()}

Possible result: f5fafd92-2298-4e72-97c9-df4dabaf27d2

random Returns a random number in range 0 <= number < max. Maximum: defaults to 1000000000 Expression: ${random(50)}

Possible result: 42

randomString Returns a random string with a specific length.
Note: This function is not reliable for password generation and should not be used for this purpose.
Length: number of characters in the string. Defaults to 10.

[a-z]: select this check box to include lowercase letters in the string.

[A-Z]: select this check box to include uppercase letters.

[0-9]: select this check box to include numbers.

Expression: ${randomString(10, true, true, true)}

Possible result: 4ptUKhHYMr

Data transformation functions

Function Description Arguments Example
base64 Encodes in base 64. none Expression: ${"username:password".base64()}

Result: dXNlcm5hbWU6cGFzc3dvcmQ=

lower Converts all the characters to lower case using the rules of the default locale. none Expression: ${"AbC".lower()}

Result: abc

length Computes the length of its input:
  • the number of characters if the input is a string
  • the number of items in a JSON array
  • the number of keys in an JSON object
none Expression: ${"Star-wars API"."Get 2 planets"."response"."body"}
Result:
{
                  "count":61,
                  "results": [
                  {"name":"Alderaan" },
                  {"name":"Naboo" },
                  {"name":"Hoth" }
                  ]
                  }

Expression: ${"Star-wars API"."Get 2 planets"."response"."body".length()}

Result: 2 Expression: ${"Star-wars API"."Get 2 planets"."response"."body"."results".length()}

Result: 3

Expression: ${"Star-wars API"."Get 2 planets"."response"."body"."results"."0"."name".length()}

Result: 8

substring Returns a truncated input. Start: position of the first character that should be extracted (0 by default).

End: position of the first character that should be omitted (end of the input by default).

Note: The index of the first character is 0.
Expression: ${"0123456".substring(1)}

Result: 123456

Expression: ${"0123456".substring(1, 2)}

Result: 1

Expression: ${"0123456".substring(0, 4)} Result: 0123

jsonPath Returns the elements extracted from the JSON input with the given JSON path selector. Selector: The JSON path selector that points to the part of the JSON to extract from the input. Expression: ${"{\"titi\": \"toto\"}".jsonPath("$.titi")}

Result: toto

xPath Returns the elements extracted from the XML input with the given XPath selector. Selector: The XPath selector that points to the part of the XML to extract from the input. Expression: ${"<title lang="en" />".xPath("//title/@lang")}

Result: [lang="en"]

hmac Creates a Hash-based Message Authentication Code . Cipher: the hash function (SHA1, SHA224, SHA256, SHA384 or SHA512). Secret

: the secret key.

Output: the output type (Base64 or Hex).

Expression: ${"test".hmac("sha256", "secret", "Base64")}

Result: Aymga2LNFrM+tnkr6MYLFY2Jou46h2/Omogeu0iMCRQ=

Expression: ${"test".hmac("sha1", "secret", "Hex")}

Result: 1aa349585ed7ecbd3b9c486a30067e395ca4b356

sha Hashes its input. Cipher: the hash function (SHA1, SHA224, SHA256, SHA384 or SHA512).

Output: the output type (Base64 or Hex).

Expression: ${"toto".sha("SHA224", "Base64")}

Result: IcBD7s1+hUI6ctrjwGKitb+g5rNc4/54jJtXpg==

Expression: ${"toto".sha("SHA1", "Hex")}

Result: 0b9c2625dc21ef05f6ad4ddf47c5f203837aa32c

md5 Calculates the MD5 hash of its input. none Expression: ${"toto".md5()}

Result: f71dbe52628a3f83a77ab494817525c6

upper Converts all the characters to upper case using the rules of the default locale. none Expression: ${"aBc".upper()}

Result: ABC

string Quotes its input. Quotes: the character that should be used to quote the input. (' or ", " is used by default). Expression: ${"toto".string()}

Result: "toto"

Expression: ${"toto".string("\"")}

Result: "toto"

Expression: ${"toto".string("'")}

Result: 'toto'

math Performs the selected operation on the input value. Operation: ADD, SUBTRACT, MULTIPLY or DIVIDE

Value: number

Expression: ${"7".math("MULTIPLY", "6")}

Result: 42

Expression: ${"3.14".math("ADD", "0.00159265")}

Result: 3.14159265

Expression: ${"100".math("DIVIDE", "-5")}

Result: -20

addTime Adds or removes a number of the selected time unit from a date. Time unit: YEAR, MONTH, WEEK, DAY, HOUR, MINUTE or SECOND

Value: integer (you can use a negative number to remove a time unit)

Expression: ${timestamp().addTime("YEAR", "1")}

Possible result: 1620292896490

Expression: ${timestamp().addTime("WEEK", "-2")}

Possible result: 1587547696437

formatDate Converts a date into the selected format. Input format: TIMESTAMP or ISO_8601

Output format: TIMESTAMP or ISO_8601

Expression: ${timestamp().formatDate("TIMESTAMP", "ISO_8601")}

Possible result: 2020-05-06T13:43:22.248Z

Expression: ${"2020-04-06T13:44:51.435Z".formatDate("ISO_8601", "TIMESTAMP")}

Result: 1586180691435