### Installing @metrichor/jmespath Source: https://github.com/nanoporetech/jmespath-ts/blob/master/README.md This snippet provides the command to install the `@metrichor/jmespath` library using npm, which is the standard package manager for Node.js. ```shell npm install @metrichor/jmespath ``` -------------------------------- ### Querying JSON with search (Advanced Examples) - JavaScript Source: https://github.com/nanoporetech/jmespath-ts/blob/master/README.md These examples demonstrate more advanced JMESPath queries using the `search` function. They cover selecting nested objects, projecting values from an array of objects, and filtering array elements based on a condition. ```javascript import { search } from '@metrichor/jmespath'; /* --- EXAMPLE 1 --- */ let JSON_DOCUMENT = { foo: { bar: { baz: [0, 1, 2, 3, 4] } } }; search(JSON_DOCUMENT, "foo.bar"); // OUTPUTS: { baz: [ 0, 1, 2, 3, 4 ] } /* --- EXAMPLE 2 --- */ JSON_DOCUMENT = { "foo": [ {"first": "a", "last": "b"}, {"first": "c", "last": "d"} ] }; search(JSON_DOCUMENT, "foo[*].first") // OUTPUTS: [ 'a', 'c' ] /* --- EXAMPLE 3 --- */ JSON_DOCUMENT = { "foo": [ {"age": 20}, {"age": 25}, {"age": 30}, {"age": 35}, {"age": 40} ] } search(JSON_DOCUMENT, "foo[?age > `30`]"); // OUTPUTS: [ { age: 35 }, { age: 40 } ] ``` -------------------------------- ### Registering Custom JMESPath Functions - JavaScript Source: https://github.com/nanoporetech/jmespath-ts/blob/master/README.md This example demonstrates how to extend JMESPath by registering a custom function, 'divide'. It shows defining the function's name, its implementation, and its input signature, allowing it to be used within JMESPath expressions. ```javascript import {search, registerFunction, TYPE_NUMBER} from '@metrichor/jmespath' search({ foo: 60, bar: 10 }, 'divide(foo, bar)') // THROWS ERROR: Error: Unknown function: divide() registerFunction( 'divide', // FUNCTION NAME (resolvedArgs) => { // CUSTOM FUNCTION const [dividend, divisor] = resolvedArgs; return dividend / divisor; }, [{ types: [TYPE_NUMBER] }, { types: [TYPE_NUMBER] }] //SIGNATURE ); search({ foo: 60,bar: 10 }, 'divide(foo, bar)'); // OUTPUTS: 6 ``` -------------------------------- ### Querying JSON with search (Basic) - JavaScript Source: https://github.com/nanoporetech/jmespath-ts/blob/master/README.md This example shows the basic usage of the `search` function to query a nested JSON object. It takes an input JSON document and a JMESPath expression to extract a specific element from an array. ```javascript search({foo: {bar: {baz: [0, 1, 2, 3, 4]}}}, "foo.bar.baz[2]") ``` -------------------------------- ### Accessing Root Value with $ Symbol - JavaScript Source: https://github.com/nanoporetech/jmespath-ts/blob/master/README.md This example demonstrates the use of the `$` symbol within a JMESPath expression to access the root JSON document. This allows for referencing values from the top level while processing nested structures. ```javascript search({foo: {bar: 999}, baz: [1, 2, 3]}, '$.baz[*].[@, $.foo.bar]') ``` -------------------------------- ### Importing search function (ES Modules) - JavaScript Source: https://github.com/nanoporetech/jmespath-ts/blob/master/README.md This code demonstrates how to import the `search` function from the `@metrichor/jmespath` library using ES module syntax, suitable for modern JavaScript environments. ```javascript import { search } from '@metrichor/jmespath'; ``` -------------------------------- ### Importing search function (CommonJS) - JavaScript Source: https://github.com/nanoporetech/jmespath-ts/blob/master/README.md This code illustrates how to import the `search` function from the `@metrichor/jmespath` library using CommonJS module syntax, typically used in Node.js environments. ```javascript const search = require('@metrichor/jmespath').search; ``` -------------------------------- ### Precompiling JMESPath expressions - JavaScript Source: https://github.com/nanoporetech/jmespath-ts/blob/master/README.md This snippet shows how to precompile a JMESPath expression into an Abstract Syntax Tree (AST) using the `compile` function. The resulting AST can then be reused with `TreeInterpreter.search` for efficient repeated queries. ```javascript import { compile, TreeInterpreter } from '@metrichor/jmespath'; const ast = compile('foo.bar'); TreeInterpreter.search(ast, {foo: {bar: 'BAZ'}}) ``` -------------------------------- ### Registering Custom JMESPath Functions (Optional Args) - JavaScript Source: https://github.com/nanoporetech/jmespath-ts/blob/master/README.md This snippet illustrates how to define custom JMESPath functions with optional arguments. By setting `optional: true` in the argument signature, a parameter can be omitted, and a default value can be provided within the function's implementation. ```javascript registerFunction( 'divide', (resolvedArgs) => { const [dividend, divisor] = resolvedArgs; return dividend / divisor ?? 1; //OPTIONAL DIVISOR THAT DEFAULTS TO 1 }, [{ types: [TYPE_NUMBER] }, { types: [TYPE_NUMBER], optional: true }] //SIGNATURE ); search({ foo: 60, bar: 10 }, 'divide(foo)'); // OUTPUTS: 60 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.