JMESPath
Last updated
Was this helpful?
Last updated
Was this helpful?
@letsflow/jmespath is a TypeScript implementation of the JMESPath spec with additional functionality for LetsFlow workflows.
JMESPath is a query language for JSON. It takes a JSON document as input and transforms it into another JSON document given a JMESPath expression.
It's important to know and understand how to use JMESPath expressions to unlock the full potential of LetsFlow. It's highly recommended that you follow the JMESPath Tutorial.
This LetsFlow fork of JMESPath extends the original specs, adding the following functionality;
Additionally, it adds the following functions:
if - Conditional expression
range - Generate a range of numbers or prefixed strings
to_object - Convert an array of key-value pairs into an object
json_serialize - Serialize a JSON value to a string
json_parse - Parse a JSON string into a JSON object
sha256 - Calculate the SHA-256 hash of a string
sha512 - Calculate the SHA-512 hash of a string
uuid - Generate a UUID
regex_test - Test if a string matches a regular expression
regex_match - Return the first match of a regular expression in a string
regex_match_all - Return all matches of a regular expression in a string
regex_replace - Replace parts of a string matching a regular expression with a replacement string
search(data: JSONValue, expression: string): JSONValue
The LetsFlow fork of JMESPath adds the following functionality, which is not in the original specs.
registerFunction(functionName: string, customFunction: RuntimeFunction, signature: InputSignature[]): void
Extend the list of built-in JMESpath expressions with your own functions.
Optional arguments are supported by setting {..., optional: true}
in argument signatures
Use $
to access the document root.
Numbers in the root scope are treated as number literals. This means that you don't need to quote numbers with backticks.
You can also use numbers in arithmetic operations
if
Syntax:
Description:
Returns thenValue
if condition
is true, otherwise returns elseValue
. If elseValue
is not provided, it defaults to null
.
Example:
range
Syntax:
Description:
Generates an array of numbers or prefixed strings from start
to end - 1
. If prefix
is provided, each number is prefixed.
Example:
to_object
Syntax:
Description: Converts an array of key-value pairs into an object.
Example:
json_serialize
Syntax:
Uses a deterministic version of JSON.stringify to serialize the value.
Description: Serializes a JSON value to a string.
Example:
json_parse
Syntax:
Description: Parses a JSON string into a JSON object.
Example:
sha256
Syntax:
Description: Calculates the SHA-256 hash of a string and returns it as a hexadecimal string.
Example:
sha512
Syntax:
Description: Calculates the SHA-512 hash of a string and returns it as a hexadecimal string.
Example:
uuid
Syntax:
Description:
Generates a UUID. If name
and (optionally) namespace
are provided, generates a version 5 UUID; otherwise, generates a version 4 UUID.
Example:
name
must be a string. Use json_serialize()
to convert a JSON object to a string.
namespace
must be a UUID string. By default, it uses the NIL UUID.
The UUID RFC pre-defines four namespaces
NameSpace_DNS: 6ba7b810-9dad-11d1-80b4-00c04fd430c8
NameSpace_URL: 6ba7b811-9dad-11d1-80b4-00c04fd430c8
NameSpace_OID: 6ba7b812-9dad-11d1-80b4-00c04fd430c8
NameSpace_X500: 6ba7b814-9dad-11d1-80b4-00c04fd430c8
You might use a UUID for a URL to create a custom namespace. For example; LetsFlow uses the following logic to create the UUID for processes.
regex_test
Syntax:
Description: Tests if a string matches a given regular expression.
Example:
regex_match
Syntax:
Description: Returns the first match of a regular expression in a string as an array.
Example:
regex_match_all
Syntax:
Description: Returns all matches of a regular expression in a string as an array of arrays.
Example:
regex_replace
Syntax:
Description: Replaces parts of a string matching a regular expression with a replacement string.
Example: