Gathering detailed insights and metrics for @letsflow/jmespath
Gathering detailed insights and metrics for @letsflow/jmespath
Gathering detailed insights and metrics for @letsflow/jmespath
Gathering detailed insights and metrics for @letsflow/jmespath
npm install @letsflow/jmespath
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (96.59%)
JavaScript (3.41%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MPL-2.0 License
151 Commits
1 Branches
1 Contributors
Updated on Mar 19, 2025
Latest Version
1.1.5-jasny.1
Package Id
@letsflow/jmespath@1.1.5-jasny.1
Unpacked Size
0.97 MB
Size
224.43 kB
File Count
51
NPM Version
10.9.2
Node Version
22.14.0
Published on
Mar 05, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
2
@letsflow/jmespath is a TypeScript implementation of the JMESPath spec.
JMESPath is a query language for JSON. It will take a JSON document as input and transform 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:
npm install @letsflow/jmespath
search(data: JSONValue, expression: string): JSONValue
1import { search } from '@letsflow/jmespath'; 2 3search( 4 { foo: { bar: { baz: [0, 1, 2, 3, 4] } } }, 5 "foo.bar.baz[2]" 6); 7 8// OUTPUTS: 2
In the example we gave the search
function input data of
{foo: {bar: {baz: [0, 1, 2, 3, 4]}}}
as well as the JMESPath
expression foo.bar.baz[2]
, and the search
function evaluated
the expression against the input data to produce the result 2
.
The JMESPath language can do a lot more than select an element from a list. Here are a few more examples:
1import { search } from '@letsflow/jmespath'; 2 3const document = { 4 foo: { 5 bar: { 6 baz: [0, 1, 2, 3, 4] 7 } 8 } 9}; 10 11search(document, "foo.bar"); 12// OUTPUTS: { baz: [ 0, 1, 2, 3, 4 ] }
1import { search } from '@letsflow/jmespath'; 2 3const document = { 4 "foo": [ 5 { "first": "a", "last": "b" }, 6 { "first": "c", "last": "d" } 7 ] 8}; 9 10search(document, "foo[*].first") 11// OUTPUTS: [ 'a', 'c' ]
1import { search } from '@letsflow/jmespath'; 2 3const document = { 4 "foo": [ 5 { "age": 20 }, 6 { "age": 25 }, 7 { "age": 30 }, 8 { "age": 35 }, 9 { "age": 40 } 10 ] 11} 12 13search(document, "foo[?age > `30`]"); 14// OUTPUTS: [ { age: 35 }, { age: 40 } ]
compile(expression: string): ExpressionNodeTree
You can precompile all your expressions ready for use later on. the compile
function takes a JMESPath expression and returns an abstract syntax tree that
can be used by the TreeInterpreter function
1import { compile, TreeInterpreter } from '@jmespath-community/jmespath'; 2 3const ast = compile('foo.bar'); 4 5TreeInterpreter.search(ast, { foo: { bar: 'BAZ' } }) 6// RETURNS: "BAZ"
registerFunction(functionName: string, customFunction: RuntimeFunction, signature: InputSignature[]): void
Extend the list of built-in JMESpath expressions with your own functions.
1 import {search, registerFunction, TYPE_NUMBER} from '@letsflow/jmespath' 2 3 search({ foo: 60, bar: 10 }, 'divide(foo, bar)') 4 // THROWS ERROR: Error: Unknown function: divide() 5 6 registerFunction( 7 'divide', // FUNCTION NAME 8 (resolvedArgs) => { // CUSTOM FUNCTION 9 const [dividend, divisor] = resolvedArgs; 10 return dividend / divisor; 11 }, 12 [{ types: [TYPE_NUMBER] }, { types: [TYPE_NUMBER] }] //SIGNATURE 13 ); 14 15 search({ foo: 60, bar: 10 }, 'divide(foo, bar)'); 16 // OUTPUTS: 6
Optional arguments are supported by setting {..., optional: true}
in argument signatures
1 registerFunction( 2 'divide', 3 (resolvedArgs) => { 4 const [dividend, divisor] = resolvedArgs; 5 return dividend / divisor ?? 1; //OPTIONAL DIVISOR THAT DEFAULTS TO 1 6 }, 7 [{ types: [TYPE_NUMBER] }, { types: [TYPE_NUMBER], optional: true }] //SIGNATURE 8 ); 9 10 search({ foo: 60, bar: 10 }, 'divide(foo)'); 11 // OUTPUTS: 60
Use $
to access the document root.
1search({foo: { bar: 999 }, baz: [1, 2, 3]}, '$.baz[*].[@, $.foo.bar]') 2 3// OUTPUTS: 4// [ [ 1, 999 ], [ 2, 999 ], [ 3, 999 ] ]
Numbers in the root scope are treated as number literals. This means that you don't need to quote numbers with backticks.
1search([{"bar": 1}, {"bar": 10}], '[?bar==10]') 2 3// OUTPUTS; 4// [{"bar": 10}]
You can also use numbers in arithmetic operations
search({}, '16 + 26'); // 42
if
Syntax:
1if(condition, thenValue, elseValue?)
Description:
Returns thenValue
if condition
is true, otherwise returns elseValue
. If elseValue
is not provided, it defaults to null
.
Example:
1if(@ > 10, "large", "small")
get
Syntax:
1get(object, key, defaultValue?)
Description: Returns the value of a key in an object.
Example:
1get({ key: 'value' }, 'key') // "value" 2get({ key: 'value' }, 'missing') // null 3get({ key: 'value' }, 'missing', 'default') // "default"
range
Syntax:
1range(start, end, prefix?)
Description:
Generates an array of numbers or prefixed strings from start
to end - 1
. If prefix
is provided, each number is prefixed.
Example:
1range(5) // [0, 1, 2, 3, 4] 2range(1, 5) // [1, 2, 3, 4] 3range(1, 5, 'item_') // ["item_1", "item_2", "item_3", "item_4"]
to_object
Syntax:
1to_object(entries)
Description:
Converts an array of key-value pairs into an object.
Example:
1to_object([['key1', 'value1'], ['key2', 'value2']]) 2// { "key1": "value1", "key2": "value2" } 3 4[ 'value1', 'value2'] | to_object(zip(range(1, length(@) + 1, 'key'), @)) 5// { "key1": "value1", "key2": "value2" }
json_serialize
Syntax:
1json_serialize(value)
Uses a deterministic version of JSON.stringify to serialize the value.
Description:
Serializes a JSON value to a string.
Example:
1json_serialize({ key: 'value' }) 2// "{\"key\":\"value\"}"
json_parse
Syntax:
1json_parse(string)
Description:
Parses a JSON string into a JSON object.
Example:
1json_parse("{\"key\":\"value\"}") 2// { "key": "value" }
sha256
Syntax:
1sha256(string)
Description:
Calculates the SHA-256 hash of a string and returns it as a hexadecimal string.
Example:
1sha256('hello') 2// "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
sha512
Syntax:
1sha512(string)
Description:
Calculates the SHA-512 hash of a string and returns it as a hexadecimal string.
Example:
1sha512('hello') 2// "9b71d224bd62f3785d96d46ad3ea3d73319b0c44e59b202205c5d235a0a6caa5a3b36f8c0ab9d45df9215bf07d4d1552c0b1f8bd2671c8a7a3d126f457d79d72"
uuid
Syntax:
1uuid(name?, namespace?)
Description:
Generates a version 5 UUID.
UUID v5 is consistent. It creates a UUID based on the SHA hash of the input. This means that any given combination of input and namespace will result in the same UUID, every time.
Example:
1uuid('example') // v5 UUID 2uuid('example', '6ba7b810-9dad-11d1-80b4-00c04fd430c8') // v5 UUID with namespace
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
6ba7b810-9dad-11d1-80b4-00c04fd430c8
6ba7b811-9dad-11d1-80b4-00c04fd430c8
6ba7b812-9dad-11d1-80b4-00c04fd430c8
6ba7b814-9dad-11d1-80b4-00c04fd430c8
regex_test
Syntax:
1regex_test(regex, string)
Description:
Tests if a string matches a given regular expression.
Example:
1regex_test('/^hello/', 'hello world') // true 2 3regex_test('/^hello/', 'HELLO world') // false 4regex_test('/^hello/i', 'HELLO world') // true
regex_match
Syntax:
1regex_match(regex, string)
Description:
Returns the first match of a regular expression in a string as an array.
Example:
1regex_match('/hello (\\w+)/', 'hello world') 2// ["hello world", "world"] 3 4regex_match('/\\w+/g', 'hello world') 5// ["hello", "world"]
regex_match_all
Syntax:
1regex_match_all(regex, string)
Description:
Returns all matches of a regular expression in a string as an array of arrays.
Example:
1regex_match_all('/(\\w+)=(\d+)/g', 'foo=24 bar=99') 2// [["foo=24", "foo", "24"], ["bar=99", "bar", "99"]]
regex_replace
Syntax:
1regex_replace(regex, replacement, string)
Description:
Replaces parts of a string matching a regular expression with a replacement string.
Example:
1regex_replace('/world/', 'universe', 'hello world') 2// "hello universe"
regex_count
Syntax:
1regex_count(regex, string)
Description: Counts the number of matches of a regular expression in a string.
Example:
1regex_count('/\\w+/g', 'hello world')
2// 2
The example above only shows a small amount 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.
No vulnerabilities found.
No security vulnerabilities found.