### Install JSONPath Plus via npm Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html Use npm to install the jsonpath-plus package. ```bash npm install jsonpath-plus ``` -------------------------------- ### JSONPath Initialization (AutoStart) Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/functions/JSONPath.html Initializes JSONPath with options that automatically start processing. Returns an array of JSONPathClass instances. ```APIDOC ## JSONPath(options: JSONPathOptionsAutoStart): JSONPathClass[] ### Description Initializes JSONPath with options that automatically start processing. Returns an array of JSONPathClass instances. ### Parameters * **options** (JSONPathOptionsAutoStart) - The configuration options for JSONPath. ### Returns * JSONPathClass[] - An array of JSONPathClass instances. ``` -------------------------------- ### Browser Setup for JSONPath Plus (UMD) Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html Include the UMD bundle for browser usage. No additional build tools are necessary. ```html ``` -------------------------------- ### Browser Setup and Usage Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md Include the UMD bundle for browser usage. Instantiate JSONPath by calling JSONPath.JSONPath with the path and JSON object. ```html ``` -------------------------------- ### Node.js Setup for JSONPath Plus Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html Import and use JSONPath in a Node.js environment. Requires the 'jsonpath-plus' package. ```javascript const {JSONPath} = require('jsonpath-plus'); const result = JSONPath({path: '...', json}); ``` -------------------------------- ### ESM Setup and Usage (Modern Browsers) Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md Import JSONPath using ES6 module syntax for modern browsers. Specify the path to the ESM bundle. ```html ``` -------------------------------- ### Serve Browser Tests Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html Start a local server to run in-browser tests for jsonpath-plus. Visit http://localhost:8082/test/ to access the tests. ```bash npm run browser-test ``` -------------------------------- ### Select all prices in store Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html This example uses recursive descent to find all 'price' properties at any level within the 'store' object. ```jsonpath $.store..price ``` -------------------------------- ### Select the first two books Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html This example selects the first two books from the 'book' array using slicing or explicit indexing. ```jsonpath $..book[:2] ``` ```jsonpath $..book[0,1] ``` -------------------------------- ### Select authors of all books Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html This example demonstrates selecting the 'author' property from all items within the 'book' array in the 'store'. ```jsonpath $.store.book[*].author ``` -------------------------------- ### JSON Data Example Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md The JSON data structure used for demonstrating JSONPath queries. ```json { "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } } } ``` -------------------------------- ### Filter books cheaper than 10 Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html This example filters the 'book' array to include only books where the 'price' is less than 10. ```jsonpath $..book[?(@.price<10)] ``` -------------------------------- ### Select categories and authors of all books Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html This example selects specific properties ('category' and 'author') from each book object in the 'book' array. ```jsonpath $..book[0][category,author] ``` -------------------------------- ### XML Data Example Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md The XML data structure used for comparing with JSONPath queries. ```xml reference Nigel Rees Sayings of the Century 8.95 fiction Evelyn Waugh Sword of Honour 12.99 fiction Herman Melville Moby Dick 0-553-21311-3 8.99 fiction J. R. R. Tolkien The Lord of the Rings 0-395-19395-8 22.99 red 19.95 ``` -------------------------------- ### Select all items in store Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html This example selects all direct properties within the 'store' object, which includes the 'book' array and the 'bicycle' object. ```jsonpath $.store.* ``` -------------------------------- ### ESM Setup and Usage (Bundlers) Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md Import JSONPath using standard ES6 module syntax when using bundlers like Rollup. Ensure 'browser' is included in mainFields for browser builds. ```javascript import {JSONPath} from 'jsonpath-plus'; const result = JSONPath({path: '...', json}); ``` -------------------------------- ### ESM Setup for JSONPath Plus (Modern Browsers) Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html Use ES6 Module imports for JSONPath Plus in modern browsers. ```html ``` -------------------------------- ### Node.js Setup and Usage Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md Import the JSONPath object from the 'jsonpath-plus' package and use it to query JSON data. Requires the 'json' object to be defined. ```javascript const {JSONPath} = require('jsonpath-plus'); const result = JSONPath({path: '...', json}); ``` -------------------------------- ### Select the last book Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html This example selects the last book from the 'book' array using negative indexing or a length calculation. ```jsonpath $..book[-1:] ``` ```jsonpath $..book[(@.length-1)] ``` -------------------------------- ### Select all authors Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html This example uses the recursive descent operator '..' to find all 'author' properties anywhere in the JSON structure. ```jsonpath $..author ``` -------------------------------- ### Select the third book Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html This example selects the third book from the 'book' array. Note that JSONPath uses 0-based indexing. ```jsonpath $..book[2] ``` -------------------------------- ### Filter books with an ISBN number Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html This example filters the 'book' array to include only those books that have an 'isbn' property. ```jsonpath $..book[?(@.isbn)] ``` -------------------------------- ### Filter properties by value Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html This example selects all property values that are not equal to 8.95, specifically targeting properties named 'price'. The '^' operator can be used to access the parent object. ```jsonpath $..*[?(@property === 'price' && @ !== 8.95)] ``` -------------------------------- ### toPathArray Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/functions/JSONPath.html Converts a normalized or unnormalized path string into an array representation. For example, a path string like '$.a.b[0]' would be converted to ['$', 'a', 'b', '0']. ```APIDOC ## toPathArray ### Description Accepts a normalized or unnormalized path as a string and converts it to an array representation. ### Parameters #### path * **path** (string) - The path string to convert. ### Returns * string[] - An array representing the path. ``` -------------------------------- ### JSONPathOptionsAutoStart Interface Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/modules.html Options related to the autostart behavior. ```APIDOC ## JSONPathOptionsAutoStart Interface ### Description Specifies settings for the automatic start of JSONPath operations. ### Properties * **enabled** (boolean) - Whether autostart is enabled. * **timeout** (number) - The timeout in milliseconds for autostart. ``` -------------------------------- ### JSONPath Initialization (Standard) Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/functions/JSONPath.html Initializes JSONPath with standard options. Returns an array of callable JSONPath instances. ```APIDOC ## JSONPath(options: JSONPathOptions): T[] ### Description Initializes JSONPath with standard options. Returns an array of callable JSONPath instances. ### Parameters * **options** (JSONPathOptions) - The configuration options for JSONPath. ### Returns * T[] - An array of callable JSONPath instances. ``` -------------------------------- ### JSONPathCallable with Options (AutoStart) Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/interfaces/JSONPathCallable.html This overload allows you to call JSONPathCallable with options that enable auto-starting the process. It returns an array of JSONPathClass instances. ```APIDOC ## JSONPathCallable(options: JSONPathOptionsAutoStart): JSONPathClass[] ### Description Invokes JSONPathCallable with auto-start options. ### Parameters * **options** (JSONPathOptionsAutoStart) - Configuration options for auto-starting JSONPath. ### Returns An array of JSONPathClass instances. ``` -------------------------------- ### JSONPathOptionsAutoStart Properties Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/interfaces/JSONPathOptionsAutoStart.html Configuration options for JSONPath queries, including path expression, result type, sandbox variables, and wrapping behavior. ```APIDOC ## JSONPathOptionsAutoStart Properties ### path - **Type**: `string | any[]` - **Description**: The JSONPath expression. Can be a string or an array of strings. ### resultType - **Type**: `"value" | "path" | "pointer" | "parent" | "parentProperty" | "all"` - **Description**: Determines the format of the returned results. Options include 'value', 'path', 'pointer', 'parent', 'parentProperty', or 'all' to return all types. - **Default**: `'value'` ### sandbox - **Type**: `Map` - **Description**: A key-value map of variables available for code evaluations within expressions. ### wrap - **Type**: `boolean` - **Description**: Controls whether the results should be wrapped in an array. If `false`, `undefined` is returned for no results, and a single result is returned directly without an array. - **Default**: `true` ``` -------------------------------- ### Run Tests on Node Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html Execute the test suite for jsonpath-plus using npm on a Node.js environment. ```bash npm test ``` -------------------------------- ### constructor Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/classes/JSONPathClass.html Initializes a new instance of the JSONPathClass. This is the primary way to create a JSONPath object. ```APIDOC ## constructor ### Description Initializes a new instance of the JSONPathClass. ### Parameters * **jsonPath** (string) - The JSONPath expression to initialize with. * **options** (object) - Optional configuration options for JSONPath evaluation. ``` -------------------------------- ### JSONPath Options Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md Details the available properties for the `options` object, which can be passed as the first argument to `JSONPath` or as properties within it. ```APIDOC ## JSONPath Options Properties ### Description These properties can be supplied on the `options` object to customize the evaluation process. ### Properties - **path** (required) - The JSONPath expression as a string or array. - **json** (required) - The JSON object to evaluate. - **autostart** (default: `true`) - If `false`, the `evaluate` method must be called manually. - **flatten** (default: `false`) - Whether to flatten the returned array of results to a single dimension. - **resultType** (default: `"value"`) - Determines the type of results returned. Can be `"value"`, `"path"`, `"pointer"`, `"parent"`, `"parentProperty"`, or `"all"`. - **sandbox** (default: `{}`) - Key-value map of variables available to code evaluations. - **wrap** (default: `true`) - Whether to wrap results in an array. If `false` and no results are found, `undefined` is returned. - **eval** (default: `"safe"`) - Script evaluation method. Options include `"safe"`, `"native"`, `false` (disable), or a custom callback function or class. - **ignoreEvalErrors** (default: `false`) - Ignore errors encountered during script evaluation. - **parent** (default: `null`) - Allows the parent of the root node to be returned. - **parentProperty** (default: `null`) - Allows the `parentProperty` of the root node to be returned. - **callback** (default: none) - A function called immediately upon retrieval of an endpoint value. - **otherTypeCallback** (default: throws error) - A callback for handling custom types using the `@other()` operator. ``` -------------------------------- ### Select price of all items Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md Use `//price` or `$.store..price` for recursive descent to find all 'price' properties within the 'store'. ```jsonpath //store//price ``` ```jsonpath $.store..price ``` -------------------------------- ### Select all items in store Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md Use `/store/*` or `$.store.*` to select all direct children of the 'store' property. ```jsonpath /store/* ``` ```jsonpath $.store.* ``` -------------------------------- ### JSONPathOptionsAutoStart Interface Properties Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/interfaces/JSONPathOptionsAutoStart.html This section details the properties available within the JSONPathOptionsAutoStart interface. These properties allow for fine-grained control over how JSONPath queries are processed and executed. ```APIDOC ## Interface: JSONPathOptionsAutoStart ### Description An interface that extends `JSONPathOptions` and provides additional configuration for JSONPath queries, specifically related to automatic execution and callback handling. ### Properties #### `autostart` (boolean) - Required * **Description**: If set to `false`, the `evaluate` method must be called manually. Otherwise, the query may execute automatically. * **Default**: `true` * **Overrides**: `JSONPathOptions.autostart` #### `callback` (JSONPathCallback) - Optional * **Description**: A callback function that is invoked immediately after a value is retrieved. It receives the payload value, its type (e.g., 'value', 'property'), and the full payload object. * **Inherited from**: `JSONPathOptions.callback` #### `eval` (boolean | typeof EvalClass | "safe" | "native" | (code: string, context: object) => any) - Optional * **Description**: Specifies how JavaScript expressions within the JSONPath query should be evaluated. Can be a boolean, a reference to `EvalClass`, specific strings like 'safe' or 'native', or a custom evaluation function. #### `flatten` (boolean) - Optional * **Description**: If `true`, the results will be flattened into a single array. #### `ignoreEvalErrors` (boolean) - Optional * **Description**: If `true`, errors during JavaScript expression evaluation will be ignored. #### `json` (string | number | boolean | object | any[]) - Required * **Description**: The JSON data structure to query. #### `otherTypeCallback` (JSONPathOtherTypeCallback) - Optional * **Description**: A callback function to handle data types other than the standard ones. #### `parent` (any) - Optional * **Description**: Specifies the parent object for the current query context. #### `parentProperty` (any) - Optional * **Description**: Specifies the property name of the parent object. #### `path` (string | any[]) - Required * **Description**: The JSONPath expression or an array of path segments to query. #### `resultType` ("value" | "path" | "pointer" | "parent" | "parentProperty" | "all") - Optional * **Description**: Determines the type of result to return. Options include 'value', 'path', 'pointer', 'parent', 'parentProperty', or 'all'. #### `sandbox` (Map) - Optional * **Description**: An object or Map to be used as the sandbox for evaluating JavaScript expressions, providing a controlled environment. #### `wrap` (boolean) - Optional * **Description**: If `true`, the results will be wrapped in an object. ``` -------------------------------- ### JSONPathClass Constructor Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/classes/JSONPathClass.html Initializes a new instance of the JSONPathClass. ```APIDOC ## constructor ### Description Initializes a new instance of the JSONPathClass. ### Returns - [JSONPathClass](JSONPathClass.html) ``` -------------------------------- ### Select First Two Books Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md Selects the first two book elements from the JSON structure. Equivalent to XPath's position-based filtering. ```xpath //book[position()<3] ``` ```jsonpath $..book[0,1] ``` ```jsonpath $..book[:2] ``` -------------------------------- ### evaluate Method (with Options Object) Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/functions/JSONPath.html The evaluate method can also accept a single options object for configuration. ```APIDOC ## evaluate(options: { callback: JSONPathCallback; json: string | number | boolean | object | any[]; otherTypeCallback: JSONPathOtherTypeCallback; path: string | any[]; }): any[] ### Description Evaluates a JSONPath expression against a JSON object using an options object for configuration. ### Parameters * **options** (object) - An object containing the path, json, callback, and otherTypeCallback. * **callback** (JSONPathCallback) - A callback function to handle matched results. * **json** (string | number | boolean | object | any[]) - The JSON data to query. * **otherTypeCallback** (JSONPathOtherTypeCallback) - A callback function to handle non-standard types. * **path** (string | any[]) - The JSONPath expression or an array representation of the path. ### Returns * any[] - An array of results. ``` -------------------------------- ### evaluate Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md Evaluates a JSONPath expression against a JSON object. This method is useful when `autostart` is set to `false` and allows for repeated evaluations with the same configuration. ```APIDOC ## evaluate(path, json, callback, otherTypeCallback) ### Description Evaluates a JSONPath expression against a JSON object. This method is only necessary if the `autostart` property is set to `false`. It can be used for repeated evaluations using the same configuration. ### Parameters - **path** (string | object) - The JSONPath expression or an object containing evaluation options. - **json** (object) - The JSON object to evaluate against. - **callback** (function) - A callback function to handle results. - **otherTypeCallback** (function) - A callback function for handling other types of results. ### Method Signature `evaluate(path, json, callback, otherTypeCallback)` OR `evaluate({path: , json: , callback: , otherTypeCallback: })` ### Notes The object-based signature can accept any other allowed instance properties (except for `autostart`). ``` -------------------------------- ### EvalClass Constructor Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/classes/EvalClass.html Initializes a new instance of the EvalClass with the provided code. ```APIDOC ## constructor EvalClass ### Description Initializes a new instance of the EvalClass. ### Signature `new EvalClass(code: string): EvalClass` ### Parameters #### Parameters - **code** (string) - The JSONPath expression code to be evaluated. ``` -------------------------------- ### JSONPathOptions Interface Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/modules.html Options for configuring JSONPath queries. ```APIDOC ## JSONPathOptions Interface ### Description Defines the available options for customizing the behavior of JSONPath queries. ### Properties * **autostart** (JSONPathOptionsAutoStart, optional) - Controls automatic query execution. * **callback** (JSONPathCallback, optional) - A callback function to process results. * **otherTypeCallback** (JSONPathOtherTypeCallback, optional) - A callback for handling different data types. * **sandbox** (object, optional) - An object for providing a custom execution environment. ``` -------------------------------- ### Select all authors Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md Use `//author` to select all 'author' properties anywhere in the JSON structure. ```jsonpath //author ``` ```jsonpath $..author ``` -------------------------------- ### JSONPath Options Interface Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/interfaces/JSONPathOptions.html This describes the structure of the options object that can be passed to JSONPath functions. ```APIDOC ## JSONPath Options This interface defines the configuration options for JSONPath queries. ### Properties * **path** (string | any[]) - Required - The JSONPath expression. * **json** (string | number | boolean | object | any[]) - Required - The JSON object to evaluate. * **resultType**? ( "value" | "path" | "pointer" | "parent" | "parentProperty" | "all" ) - Optional - Determines the format of the results. Defaults to 'value'. * **wrap**? (boolean) - Optional - Whether to wrap results in an array. Defaults to true. * **ignoreEvalErrors**? (boolean) - Optional - Ignore errors during expression evaluation. Defaults to false. * **parent**? (any) - Optional - The parent of the root node, if applicable. * **parentProperty**? (any) - Optional - The parent property name of the root node, if applicable. * **sandbox**? (Map) - Optional - A key-value map of variables for evaluation. * **otherTypeCallback**? ([JSONPathOtherTypeCallback](../types/JSONPathOtherTypeCallback.html)) - Optional - Callback for handling custom types with the `@other()` operator. ``` -------------------------------- ### runInNewContext Method Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/classes/EvalClass.html Executes the JSONPath expression in a new, isolated context. ```APIDOC ## runInNewContext ### Description Executes the JSONPath expression within a new context. ### Signature `runInNewContext(context: object): any` ### Parameters #### Parameters - **context** (object) - The context object in which to evaluate the expression. ### Returns - **any** - The result of the JSONPath expression evaluation. ``` -------------------------------- ### Filter Books by Price Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md Filters all book elements to include only those where the 'price' property is less than 10. ```xpath //book[price<10] ``` ```jsonpath $..book[?(@.price<10)] ``` -------------------------------- ### toPointer Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/functions/JSONPath.html Converts a path array into a JSON Pointer string. Internal characters like '~' and '/' are escaped according to the JSON Pointer specification. ```APIDOC ## toPointer ### Description Accepts a path array and converts it to a JSON Pointer string, with necessary characters escaped. ### Parameters #### path * **path** (string[]) - The array representing the path. ### Returns * any - A JSON Pointer string. ``` -------------------------------- ### Select the third book Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md Use array indexing `[n]` to select a specific element. Note that JSONPath uses 0-based indexing, so `[3]` selects the fourth element, while `[2]` selects the third. ```jsonpath //book[3] ``` ```jsonpath $..book[2] ``` -------------------------------- ### evaluate Method (with Callbacks) Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/functions/JSONPath.html The evaluate method allows for JSONPath evaluation using explicit path, JSON, and callback parameters. ```APIDOC ## evaluate(path: string | any[], json: string | number | boolean | object | any[], callback: JSONPathCallback, otherTypeCallback: JSONPathOtherTypeCallback): any[] ### Description Evaluates a JSONPath expression against a JSON object using callback functions for handling results and other types. ### Parameters * **path** (string | any[]) - The JSONPath expression or an array representation of the path. * **json** (string | number | boolean | object | any[]) - The JSON data to query. * **callback** (JSONPathCallback) - A callback function to handle matched results. * **otherTypeCallback** (JSONPathOtherTypeCallback) - A callback function to handle non-standard types. ### Returns * any[] - An array of results. ``` -------------------------------- ### Select all members beneath root Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html The '$..' syntax selects all members (properties and array elements) at any level beneath the root of the JSON structure. ```jsonpath '$..' ``` ```jsonpath '$..*' ``` -------------------------------- ### evaluate Method Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html The evaluate method is used to evaluate a JSONPath expression against a JSON object. It can be called directly if `autostart` is set to `false`, or used for repeated evaluations with the same configuration. ```APIDOC ## evaluate(path, json, callback, otherTypeCallback) ### Description Evaluates a JSONPath expression against a JSON object. This method is primarily used when `autostart` is set to `false`. ### Method `evaluate` ### Parameters * **path** (string | string[]) - Required - The JSONPath expression. * **json** (object) - Required - The JSON object to evaluate. * **callback** (function) - Optional - A callback function invoked upon retrieval of an endpoint value. * **otherTypeCallback** (function) - Optional - A callback function to handle custom types. ## evaluate({path: , json: , callback: , otherTypeCallback: }) ### Description An alternative signature for the `evaluate` method that accepts an options object. This pattern can also accept any other allowed instance properties. ### Method `evaluate` ### Parameters * **path** (string | string[]) - Required - The JSONPath expression. * **json** (object) - Required - The JSON object to evaluate. * **callback** (function) - Optional - A callback function invoked upon retrieval of an endpoint value. * **otherTypeCallback** (function) - Optional - A callback function to handle custom types. * **[other properties]** - Optional - Any other allowed instance properties (except `autostart`). ``` -------------------------------- ### Select Book Categories and Authors Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md Retrieves the category and author properties from all book elements. This demonstrates selecting multiple properties within a filtered set. ```xpath //book/*[self::category|self::author] ``` ```xpath //book/(category,author) ``` ```jsonpath $..book[0][category,author] ``` -------------------------------- ### toPointer Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/classes/JSONPathClass.html Converts the JSONPath expression into a JSON Pointer string. ```APIDOC ## toPointer ### Description Converts the JSONPath expression into a JSON Pointer string. ### Returns * (string) - The JSON Pointer string representation. ``` -------------------------------- ### JSONPathCallable with Path, JSON, and Callbacks Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/interfaces/JSONPathCallable.html This overload is used for direct invocation with a path, JSON data, and specific callback functions for handling results and other types. ```APIDOC ## JSONPathCallable(path: string | any[], json: string | number | boolean | object | any[], callback: JSONPathCallback, otherTypeCallback: JSONPathOtherTypeCallback): T[] ### Description Invokes JSONPathCallable with a path, JSON data, and callback functions. ### Parameters * **path** (string | any[]) - The JSONPath expression or an array of expressions. * **json** (string | number | boolean | object | any[]) - The JSON data to query. * **callback** (JSONPathCallback) - A callback function to handle results. * **otherTypeCallback** (JSONPathOtherTypeCallback) - A callback function to handle other types of results. ### Returns An array of results typed as T. ``` -------------------------------- ### Import JSONPath for ESM Bundlers Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html Use this import statement when bundling your JavaScript with tools like Rollup. Ensure 'browser' is included in mainFields for browser builds. ```javascript import {JSONPath} from 'jsonpath-plus'; const result = JSONPath({path: '...', json}); ``` -------------------------------- ### Select the last book Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md Use `[last()]` or `[-1:]` to select the last element in an array. The `last()` function is a common extension. ```jsonpath //book[last()] ``` ```jsonpath $..book[(@.length-1)] ``` ```jsonpath $..book[-1:] ``` -------------------------------- ### JSONPathCallable with Options Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/interfaces/JSONPathCallable.html This overload allows calling JSONPathCallable with general options, returning results typed according to the generic type parameter T. ```APIDOC ## JSONPathCallable(options: JSONPathOptions): T[] ### Description Invokes JSONPathCallable with specified options. ### Parameters * **options** (JSONPathOptions) - Configuration options for JSONPath. ### Returns An array of results typed as T. ``` -------------------------------- ### Select Property Names Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html Selects the property names of sub-objects. This is useful for wildcard properties and is analogous to XPath's `name()` function on properties. ```jsonpath $.store.*~ ``` -------------------------------- ### JSONPath.cache Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md Exposes the cache object, allowing users to preserve and reuse it for optimization purposes. ```APIDOC ## JSONPath.cache ### Description Exposes the cache object for those who wish to preserve and reuse it for optimization purposes. ### Property `JSONPath.cache` ``` -------------------------------- ### JSONPathClass Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/modules.html A class for managing and executing JSONPath queries. ```APIDOC ## JSONPathClass ### Description Provides a class-based approach to JSONPath querying, allowing for instantiation and reuse of query configurations. ### Methods * `constructor(options?: JSONPathOptions)` - Initializes a new instance of JSONPathClass with optional configuration. * `query(path: string, obj: any): any[]` - Executes a JSONPath query against a given object. ``` -------------------------------- ### JSONPath Function Signature Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md The main function to evaluate JSONPath expressions. It can be called with options, path, json, and optional callbacks, or these can be passed as properties of the options object. ```APIDOC ## JSONPath Function ### Description Evaluates a JSONPath expression against a JSON object. ### Signature ```javascript const result = JSONPath([options,] path, json, callback, otherTypeCallback); ``` ### Parameters - **options** (object) - Optional. An object containing configuration properties. If provided, `path`, `json`, `callback`, and `otherTypeCallback` can be set as properties within this object. - **path** (string | array) - Required. The JSONPath expression. - **json** (object | array | null | boolean | number | string) - Required. The JSON object to evaluate. - **callback** (function) - Optional. A function to be called for each discovered item. - **otherTypeCallback** (function) - Optional. A callback for handling custom types defined with `@other()`. ``` -------------------------------- ### toPathString Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/classes/JSONPathClass.html Converts the JSONPath expression into its string representation. ```APIDOC ## toPathString ### Description Converts the JSONPath expression into its string representation. ### Returns * (string) - The string representation of the JSONPath expression. ``` -------------------------------- ### JSONPathClass.toPointer Method Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/classes/JSONPathClass.html Converts a JSONPath array into a JSON Pointer string. ```APIDOC ## toPointer ### Description Accepts a path array and converts it to a JSON Pointer. The string will be in a form like: `/aProperty/anotherProperty/0` (with any `~` and `/` internal characters escaped as per the JSON Pointer spec). The JSONPath terminal constructions `~` and `^` and type operators like `@string()` are silently stripped. ### Parameters - **path**: `string[]` - The array representing the JSONPath. ### Returns `any` - A JSON Pointer string. ``` -------------------------------- ### JSONPath Function Signature Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html This is the full signature for the JSONPath function. Arguments can be passed directly or as properties of an options object. ```javascript const result = JSONPath([options,] path, json, callback, otherTypeCallback); ``` -------------------------------- ### toPathString Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/functions/JSONPath.html Converts a path array into a normalized path string. This function also strips out terminal constructions like '~' and '^', as well as type operators. ```APIDOC ## toPathString ### Description Accepts a path array and converts it to a normalized path string. Terminal constructions and type operators are silently stripped. ### Parameters #### path * **path** (string[]) - The array representing the path. ### Returns * string - A normalized path string. ``` -------------------------------- ### JSONPathOptions Interface Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/interfaces/JSONPathOptions.html The JSONPathOptions interface defines the configuration object that can be passed to JSONPath functions to customize their behavior. ```APIDOC ## Interface: JSONPathOptions This interface defines the configuration options for JSONPath operations. ### Properties * **autostart?** (boolean) - Optional. If set to `false`, the `evaluate` method must be called manually. Defaults to `true`. * **callback?** ([JSONPathCallback](../types/JSONPathCallback.html)) - Optional. A callback function that is invoked immediately upon retrieval of an endpoint value. It receives the value, type, and full payload object. * **eval?** (boolean | typeof [EvalClass](../classes/EvalClass.html) | "safe" | "native" | (code: string, context: object) => any) - Optional. Specifies the method for script evaluation within JSONPath expressions. Options include `true` (safe), `false` (disable), `"safe"`, `"native"`, a custom callback function, or an `EvalClass` instance. Defaults to `"safe"`. * **flatten?** (boolean) - Optional. Determines whether the returned array of results should be flattened into a single-dimensional array. * **ignoreEvalErrors?** (boolean) - Optional. If `true`, errors during script evaluation will be ignored. * **json** (string | number | boolean | object | any[]) - Required. The JSON data to query. * **otherTypeCallback?** ([JSONPathOtherTypeCallback](../types/JSONPathOtherTypeCallback.html)) - Optional. A callback for handling other types of data. * **parent?** (any) - Optional. Specifies the parent object for the current path. * **parentProperty?** (any) - Optional. Specifies the property name of the parent. * **path** (string | any[]) - Required. The JSONPath expression or an array of path segments. * **resultType?** ("value" | "path" | "pointer" | "parent" | "parentProperty" | "all") - Optional. Specifies the type of result to return. Defaults to `"value"`. * **sandbox?** (Map) - Optional. An object to use as the sandbox for script evaluation. * **wrap?** (boolean) - Optional. If `true`, the results will be wrapped in an array. ``` -------------------------------- ### JSONPath Function Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/modules.html The main JSONPath function for querying JSON data. ```APIDOC ## JSONPath Function ### Description Provides the primary interface for executing JSONPath queries. ### Signature `JSONPath(path: string, obj: any, options?: JSONPathOptions): any[]` ### Parameters * **path** (string) - The JSONPath expression to evaluate. * **obj** (any) - The JSON object to query. * **options** (JSONPathOptions, optional) - Configuration options for the query. ``` -------------------------------- ### Filter by Property Regex Match and Select Parent Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html Filters elements based on a regular expression match against their property name and then selects the parent. This is analogous to XPath 2.0's `matches(name(), ...)` combined with `parent::`. ```jsonpath $..book.*[?(@property.match(/bn$/i))]^ ``` -------------------------------- ### Filter Properties by Name and Value Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md Obtains all property values from any object where the property name is 'price' and its value is not equal to 8.95. Use '^' to access the parent object. ```xpath //*[name() = 'price' and . != 8.95] ``` ```jsonpath $..*[?(@property === 'price' && @ !== 8.95)] ``` -------------------------------- ### Select Parent Elements Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html Selects the parent of elements that match the criteria. This is analogous to XPath's `..` operator. ```jsonpath //*[price>19]/.. ``` ```jsonpath $..[?(@.price>19)]^ ``` -------------------------------- ### JSONPath Function Signature Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md The full signature for the JSONPath function. Arguments can be passed directly or as properties of an options object. ```javascript const result = JSONPath([options,] path, json, callback, otherTypeCallback); ``` -------------------------------- ### EvalClass Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/modules.html A class for evaluating JSONPath expressions. ```APIDOC ## EvalClass ### Description Represents an evaluator for JSONPath expressions, allowing for more complex operations and state management. ### Methods * `eval(path: string, obj: any, options?: JSONPathOptions): any[]` - Evaluates a JSONPath expression. * `JSONPath(path: string, obj: any, options?: JSONPathOptions): any[]` - Static method to create an instance and evaluate. ``` -------------------------------- ### evaluate Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/classes/JSONPathClass.html Evaluates the JSONPath expression against a given JSON object. ```APIDOC ## evaluate ### Description Evaluates the JSONPath expression against a given JSON object. ### Parameters * **json** (object) - The JSON object to evaluate against. * **options** (object) - Optional configuration options for this specific evaluation. ``` -------------------------------- ### JSONPathClass.cache Property Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/classes/JSONPathClass.html Exposes the cache object for optimization purposes. ```APIDOC ## cache ### Description Exposes the cache object for those who wish to preserve and reuse it for optimization purposes. ### Type `any` ``` -------------------------------- ### toPathArray Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/classes/JSONPathClass.html Converts the JSONPath expression into an array of path segments. ```APIDOC ## toPathArray ### Description Converts the JSONPath expression into an array of path segments. ### Returns * (Array) - An array representing the path segments. ``` -------------------------------- ### JSONPath Evaluation (with Callbacks) Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/functions/JSONPath.html Evaluates a JSONPath expression against a JSON object using callback functions for handling results and other types. ```APIDOC ## JSONPath(path: string | any[], json: string | number | boolean | object | any[], callback: JSONPathCallback, otherTypeCallback: JSONPathOtherTypeCallback): T[] ### Description Evaluates a JSONPath expression against a JSON object using callback functions for handling results and other types. ### Parameters * **path** (string | any[]) - The JSONPath expression or an array representation of the path. * **json** (string | number | boolean | object | any[]) - The JSON data to query. * **callback** (JSONPathCallback) - A callback function to handle matched results. * **otherTypeCallback** (JSONPathOtherTypeCallback) - A callback function to handle non-standard types. ### Returns * T[] - An array of results. ``` -------------------------------- ### Convert Path Array to String Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html Use JSONPath.toPathString to convert a path array back into a normalized path string. Terminal constructions and type operators are stripped. ```javascript JSONPath.toPathString(['$', 'store', 'book', '[*]', 'author']) ``` -------------------------------- ### Filter Books with ISBN Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md Filters all book elements to include only those that possess an 'isbn' property. This uses a property existence check. ```xpath //book[isbn] ``` ```jsonpath $..book[?(@.isbn)] ``` -------------------------------- ### JSONPath.toPointer Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md Converts a JSONPath array into a JSON Pointer string, escaping special characters as per RFC 6901. ```APIDOC ## JSONPath.toPointer(pathAsArray) ### Description Accepts a path array and converts it to a [JSON Pointer](https://tools.ietf.org/html/rfc6901). The string will be in a form like: `/aProperty/anotherProperty/0` (with any `~` and `/` internal characters escaped as per the JSON Pointer spec). The JSONPath terminal constructions `~` and `^` and type operators like `@string()` are silently stripped. ### Parameters - **pathAsArray** (Array) - The path array to convert. ### Returns - (string) - A JSON Pointer string. ### Example `JSONPath.toPointer(['$', 'a', 'b', 0])` would return `/a/b/0`. ``` -------------------------------- ### JSONPath.toPathString Source: https://github.com/jsonpath-plus/jsonpath/blob/main/README.md Converts a JSONPath array into a normalized path string. ```APIDOC ## JSONPath.toPathString(pathAsArray) ### Description Accepts a path array and converts it to a normalized path string. The string will be in a form like: `$['aProperty']['anotherProperty'][0]`. The JSONPath terminal constructions `~` and `^` and type operators like `@string()` are silently stripped. ### Parameters - **pathAsArray** (Array) - The path array to convert. ### Returns - (string) - A normalized JSONPath string. ### Example `JSONPath.toPathString(['$', 'a', 'b', 0])` would return `$['a']['b'][0]`. ``` -------------------------------- ### Select Numeric Values Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html Selects all numeric values within a specified path. This utilizes type-casting selectors like `@number()`. ```jsonpath $..book..*@number() ``` -------------------------------- ### JSONPathCallable Interface Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/modules.html Defines the callable signature for JSONPath operations. ```APIDOC ## JSONPathCallable Interface ### Description This interface describes objects that can be called like a function to perform JSONPath queries. ### Signature `(path: string, obj: any, options?: JSONPathOptions): any[]` ``` -------------------------------- ### JSONPathClass.toPathString Method Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/classes/JSONPathClass.html Converts a JSONPath array into a normalized string representation. ```APIDOC ## toPathString ### Description Accepts a path array and converts it to a normalized path string. The string will be in a form like: `$['aProperty']['anotherProperty'][0]`. The JSONPath terminal constructions `~` and `^` and type operators like `@string()` are silently stripped. ### Parameters - **path**: `string[]` - The array representing the JSONPath. ### Returns `string` - A normalized JSONPath string. ``` -------------------------------- ### Filter by Root Object Property Value Source: https://github.com/jsonpath-plus/jsonpath/blob/main/docs/ts/index.html Filters elements by comparing their property value to a value from the root object. This is analogous to XPath's variable referencing. ```jsonpath $..book[?(@.price === @root.store.book[2].price)] ```