JMESPath
@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.
This fork 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
Installation
Usage
search(data: JSONValue, expression: string): JSONValue
search(data: JSONValue, expression: string): JSONValue
The example above only shows a small example of what a JMESPath expression can do. If you want to take a tour of the language, the best place to go is the JMESPath Tutorial.
The full JMESPath specification can be found on the JMESPath site.
Extension to the original specs
The LetsFlow fork of JMESPath adds the following functionality, which is not in the original specs.
Custom functions
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
Root value access
Use $
to access the document root.
Number literals
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
Additional Functions
if
if
Syntax:
Description:
Returns thenValue
if condition
is true, otherwise returns elseValue
. If elseValue
is not provided, it defaults to null
.
Example:
range
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
to_object
Syntax:
Description: Converts an array of key-value pairs into an object.
Example:
json_serialize
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
json_parse
Syntax:
Description: Parses a JSON string into a JSON object.
Example:
sha256
sha256
Syntax:
Description: Calculates the SHA-256 hash of a string and returns it as a hexadecimal string.
Example:
sha512
sha512
Syntax:
Description: Calculates the SHA-512 hash of a string and returns it as a hexadecimal string.
Example:
uuid
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.
regex_test
regex_test
Syntax:
Description: Tests if a string matches a given regular expression.
Example:
regex_match
regex_match
Syntax:
Description: Returns the first match of a regular expression in a string as an array.
Example:
regex_match_all
regex_match_all
Syntax:
Description: Returns all matches of a regular expression in a string as an array of arrays.
Example:
regex_replace
regex_replace
Syntax:
Description: Replaces parts of a string matching a regular expression with a replacement string.
Example:
Last updated