Exposes the cache object for those who wish to preserve and reuse +
Exposes the cache object for those who wish to preserve and reuse it for optimization purposes.
-Accepts a path array and converts 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.
(see also licenses for dev. deps.)
-Analyse, transform, and selectively extract data from JSON +
+(see also licenses for dev. deps.)
+Analyse, transform, and selectively extract data from JSON documents (and JavaScript objects).
jsonpath-plus expands on the original specification to add some
additional operators and makes explicit some behaviors the original
did not spell out.
Try the browser demo or -Runkit (Node).
+Try the browser demo or +Runkit (Node).
Please note: This project is not currently being actively maintained. We may accept well-documented PRs or some simple updates, but are not looking to make fixes or add new features ourselves.
-jsonpath-plus is consistently performant with both large and small datasets compared to other json querying libraries per json-querying-performance-testing. You can verify these findings by running the project yourself and adding more perf cases.
npm install jsonpath-plus
+Benchmarking
jsonpath-plus is consistently performant with both large and small datasets compared to other json querying libraries per json-querying-performance-testing. You can verify these findings by running the project yourself and adding more perf cases.
+Install
npm install jsonpath-plus
-Setup
Node.js
const {JSONPath} = require('jsonpath-plus');
const result = JSONPath({path: '...', json});
+Setup
Node.js
const {JSONPath} = require('jsonpath-plus');
const result = JSONPath({path: '...', json});
-Browser
For browser usage you can directly include dist/index-browser-umd.cjs; no
+
Browser
For browser usage you can directly include dist/index-browser-umd.cjs; no
Browserify magic is necessary:
<script src="node_modules/jsonpath-plus/dist/index-browser-umd.cjs"></script>
<script>
const result = JSONPath.JSONPath({path: '...', json: {}});
</script>
-ESM (Modern browsers)
You may also use ES6 Module imports (for modern browsers):
+ESM (Modern browsers)
You may also use ES6 Module imports (for modern browsers):
<script type="module">
import {
JSONPath
} from './node_modules/jsonpath-plus/dist/index-browser-esm.js';
const result = JSONPath({path: '...', json: {}});
</script>
-ESM (Bundlers)
Or if you are bundling your JavaScript (e.g., with Rollup), just use,
-noting that mainFields
+
ESM (Bundlers)
Or if you are bundling your JavaScript (e.g., with Rollup), just use,
+noting that mainFields
should include browser for browser builds (for Node, the default, which
checks module, should be fine):
import {JSONPath} from 'jsonpath-plus';
const result = JSONPath({path: '...', json});
-Usage
The full signature available is:
+Usage
The full signature available is:
const result = JSONPath([options,] path, json, callback, otherTypeCallback);
@@ -89,8 +89,8 @@
the callback function being executed 0 to N times depending
on the number of independent items to be found in the result.
See the docs below for more on JSONPath's available arguments.
-See also the API docs.
-Properties
The properties that can be supplied on the options object or
+
See also the API docs.
+Properties
The properties that can be supplied on the options object or
evaluate method (as the first argument) include:
- path (required) - The JSONPath expression as a (normalized
@@ -104,7 +104,7 @@
- resultType (default: "value") - Can be case-insensitive form of
"value", "path", "pointer", "parent", or "parentProperty" to determine
respectively whether to return results as the values of the found items,
-as their absolute paths, as JSON Pointers
+as their absolute paths, as JSON Pointers
to the absolute paths, as their parent objects, or as their parent's
property name. If set to "all", all of these types will be returned on
an object with the type as key name.
@@ -157,7 +157,7 @@
belongs to the "other" type or not (or it may handle transformations and
return false).
-Instance methods
+Instance methods
- evaluate(path, json, callback, otherTypeCallback) OR
evaluate({path: <path>, json: <json object>, callback:
<callback function>, otherTypeCallback:
@@ -168,7 +168,7 @@
accept any of the other allowed instance properties (except
for
autostart which would have no relevance here).
-Class properties and methods
+Class properties and methods
- JSONPath.cache - Exposes the cache object for those who wish
to preserve and reuse it for optimization purposes.
- JSONPath.toPathArray(pathAsString) - Accepts a normalized or
@@ -180,13 +180,13 @@
constructions
~ and ^ and type operators like @string() are
silently stripped.
- JSONPath.toPointer(pathAsArray) - Accepts a path array and
-converts to a JSON Pointer.
+converts 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.
-Syntax through examples
Given the following JSON, taken from http://goessner.net/articles/JsonPath/:
+Syntax through examples
Given the following JSON, taken from http://goessner.net/articles/JsonPath/:
{
"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
}
}
}
@@ -228,7 +228,7 @@
Please note that the XPath examples below do not distinguish between
retrieving elements and their text content (except where useful for
comparisons or to prevent ambiguity). Note: to test the XPath examples
-(including 2.0 ones), this demo
+(including 2.0 ones), this demo
may be helpful (set to xml or xml-strict).
@@ -241,160 +241,160 @@
-/store/book/author
-$.store.book[*].author
+/store/book/author
+$.store.book[*].author
The authors of all books in the store
Can also be represented without the $. as store.book[*].author (though this is not present in the original spec); note that some character literals ($ and @) require escaping, however
-//author
-$..author
+//author
+$..author
All authors
-/store/*
-$.store.*
+/store/*
+$.store.*
All things in store, which are its books (a book array) and a red bicycle (a bicycle object).
-/store//price
-$.store..price
+/store//price
+$.store..price
The price of everything in the store.
-//book[3]
-$..book[2]
+//book[3]
+$..book[2]
The third book (book object)
-//book[last()]
-$..book[(@.length-1)]
$..book[-1:]
+//book[last()]
+$..book[(@.length-1)]
$..book[-1:]
The last book in order.
To access a property with a special character, utilize [(@['...'])] for the filter (this particular feature is not present in the original spec)
-//book[position()<3]
-$..book[0,1]
$..book[:2]
+//book[position()<3]
+$..book[0,1]
$..book[:2]
The first two books
-//book/*[self::category|self::author] or //book/(category,author) in XPath 2.0
-$..book[0][category,author]
+//book/*[self::category|self::author] or //book/(category,author) in XPath 2.0
+$..book[0][category,author]
The categories and authors of all books
-//book[isbn]
-$..book[?(@.isbn)]
+//book[isbn]
+$..book[?(@.isbn)]
Filter all books with an ISBN number
To access a property with a special character, utilize [?@['...']] for the filter (this particular feature is not present in the original spec)
-//book[price<10]
-$..book[?(@.price<10)]
+//book[price<10]
+$..book[?(@.price<10)]
Filter all books cheaper than 10
-//*[name() = 'price' and . != 8.95]
-$..*[?(@property === 'price' && @ !== 8.95)]
+//*[name() = 'price' and . != 8.95]
+$..*[?(@property === 'price' && @ !== 8.95)]
Obtain all property values of objects whose property is price and which does not equal 8.95
With the bare @ allowing filtering objects by property value (not necessarily within arrays), you can add ^ after the expression to get at the object possessing the filtered properties
-/
-$
+/
+$
The root of the JSON object (i.e., the whole object itself)
To get a literal $ (by itself or anywhere in the path), you must use the backtick escape
-//*/*|//*/*/text()
-$..*
+//*/*|//*/*/text()
+$..*
All Elements (and text) beneath root in an XML document. All members of a JSON structure beneath the root.
-//*
-$..
+//*
+$..
All Elements in an XML document. All parent components of a JSON structure including root.
This behavior was not directly specified in the original spec
-//*[price>19]/..
-$..[?(@.price>19)]^
+//*[price>19]/..
+$..[?(@.price>19)]^
Parent of those specific items with a price greater than 19 (i.e., the store value as the parent of the bicycle and the book array as parent of an individual book)
Parent (caret) not present in the original spec
-/store/*/name() (in XPath 2.0)
-$.store.*~
+/store/*/name() (in XPath 2.0)
+$.store.*~
The property names of the store sub-object ("book" and "bicycle"). Useful with wildcard properties.
Property name (tilde) is not present in the original spec
-/store/book[not(. is /store/book[1])] (in XPath 2.0)
-$.store.book[?(@path !== "$['store']['book'][0]")]
+/store/book[not(. is /store/book[1])] (in XPath 2.0)
+$.store.book[?(@path !== "$['store']['book'][0]")]
All books besides that at the path pointing to the first
-@path not present in the original spec
+@path is not present in the original spec
-//book[parent::*/bicycle/color = "red"]/category
-$..book[?(@parent.bicycle && @parent.bicycle.color === "red")].category
+//book[parent::*/bicycle/color = "red"]/category
+$..book[?(@parent.bicycle && @parent.bicycle.color === "red")].category
Grabs all categories of books where the parent object of the book has a bicycle child whose color is red (i.e., all the books)
-@parent is not present in the original spec
+@parent is not present in the original spec
-//book/*[name() != 'category']
-$..book.*[?(@property !== "category")]
+//book/*[name() != 'category']
+$..book.*[?(@property !== "category")]
Grabs all children of "book" except for "category" ones
-@property is not present in the original spec
+@property is not present in the original spec
-//book[position() != 1]
-$..book[?(@property !== 0)]
+//book[position() != 1]
+$..book[?(@property !== 0)]
Grabs all books whose property (which, being that we are reaching inside an array, is the numeric index) is not 0
-@property is not present in the original spec
+@property is not present in the original spec
-/store/*/*[name(parent::*) != 'book']
-$.store.*[?(@parentProperty !== "book")]
+/store/*/*[name(parent::*) != 'book']
+$.store.*[?(@parentProperty !== "book")]
Grabs the grandchildren of store whose parent property is not book (i.e., bicycle's children, "color" and "price")
-@parentProperty is not present in the original spec
+@parentProperty is not present in the original spec
-//book[count(preceding-sibling::*) != 0]/*/text()
-$..book.*[?(@parentProperty !== 0)]
+//book[count(preceding-sibling::*) != 0]/*/text()
+$..book.*[?(@parentProperty !== 0)]
Get the property values of all book instances whereby the parent property of these values (i.e., the array index holding the book item parent object) is not 0
-@parentProperty is not present in the original spec
+@parentProperty is not present in the original spec
-//book[price = /store/book[3]/price]
-$..book[?(@.price === @root.store.book[2].price)]
+//book[price = /store/book[3]/price]
+$..book[?(@.price === @root.store.book[2].price)]
Filter all books whose price equals the price of the third book
-@root is not present in the original spec
+@root is not present in the original spec
-//book/../*[. instance of element(*, xs:decimal)] (in XPath 2.0)
-$..book..*@number()
+//book/../*[. instance of element(*, xs:decimal)] (in XPath 2.0)
+$..book..*@number()
Get the numeric values within the book array
-@number(), the other basic types (@boolean(), @string()), other low-level derived types (@null(), @object(), @array()), the JSONSchema-added type, @integer(), the compound type @scalar() (which also accepts undefined and non-finite numbers for JavaScript objects as well as all of the basic non-object/non-function types), the type, @other(), to be used in conjunction with a user-defined callback (see otherTypeCallback) and the following non-JSON types that can nevertheless be used with JSONPath when querying non-JSON JavaScript objects (@undefined(), @function(), @nonFinite()) are not present in the original spec
+@number(), the other basic types (@boolean(), @string()), other low-level derived types (@null(), @object(), @array()), the JSONSchema-added type, @integer(), the compound type @scalar() (which also accepts undefined and non-finite numbers for JavaScript objects as well as all of the basic non-object/non-function types), the type, @other(), to be used in conjunction with a user-defined callback (see otherTypeCallback) and the following non-JSON types that can nevertheless be used with JSONPath when querying non-JSON JavaScript objects (@undefined(), @function(), @nonFinite()) are not present in the original spec
-//book/*[name() = 'category' and matches(., 'tion$')] (XPath 2.0)
-$..book.*[?(@property === "category" && @.match(/TION$/i))]
+//book/*[name() = 'category' and matches(., 'tion$')] (XPath 2.0)
+$..book.*[?(@property === "category" && @.match(/TION$/i))]
All categories of books which match the regex (end in 'TION' case insensitive)
-@property is not present in the original spec.
+@property is not present in the original spec.
-//book/[matches(name(), 'bn$')]/parent:: (XPath 2.0)
-$..book.*[?(@property.match(/bn$/i))]^
+//book/*[matches(name(), 'bn$')]/parent::* (XPath 2.0)
+$..book.*[?(@property.match(/bn$/i))]^
All books which have a property matching the regex (end in 'TION' case insensitive)
-@property is not present in the original spec. Note: Uses the parent selector ^ at the end of the expression to return to the parent object; without the parent selector, it matches the two isbn key values.
+@property is not present in the original spec. Note: Uses the parent selector ^ at the end of the expression to return to the parent object; without the parent selector, it matches the two isbn key values.
@@ -407,7 +407,7 @@
Any additional variables supplied as properties on the optional "sandbox"
object option are also available to (parenthetical-based)
evaluations.
-Potential sources of confusion for XPath users
+Potential sources of confusion for XPath users
- In JSONPath, a filter expression, in addition to its
@ being a
reference to its children, actually selects the immediate children
as well, whereas in XPath, filter conditions do not select the children
@@ -417,14 +417,14 @@
- In JSONPath, equality tests utilize (as per JavaScript) multiple equal signs
whereas in XPath, they use a single equal sign.
-Command line interface
A basic command line interface (CLI) is provided. Access it using npx jsonpath-plus <json-file> <jsonpath-query>.
-Ideas
+Command line interface
A basic command line interface (CLI) is provided. Access it using npx jsonpath-plus <json-file> <jsonpath-query>.
+Ideas
- Support OR outside of filters (as in XPath
|) and grouping.
- Create syntax to work like XPath filters in not selecting children?
- Allow option for parentNode equivalent (maintaining entire chain of
parent-and-parentProperty objects up to root)
-Development
Running the tests on Node:
+Development
Running the tests on Node:
npm test
@@ -436,7 +436,7 @@
-- Visit http://localhost:8082/test/.
+- Visit http://localhost:8082/test/.
-License
-
+License
+
diff --git a/docs/ts/interfaces/JSONPathCallable.html b/docs/ts/interfaces/JSONPathCallable.html
index cc52eb0..e19b1b3 100644
--- a/docs/ts/interfaces/JSONPathCallable.html
+++ b/docs/ts/interfaces/JSONPathCallable.html
@@ -1 +1 @@
-JSONPathCallable | jsonpath-plus Interface JSONPathCallable
- JSONPath
Callable<T>(options): JSONPathClass Type Parameters
Parameters
- options: JSONPathOptionsAutoStart
Returns JSONPathClass
- JSONPath
Callable<T>(options): T Type Parameters
Parameters
- options: JSONPathOptions
Returns T
- JSONPath
Callable<T>(path, json, callback, otherTypeCallback): T Type Parameters
Parameters
- path: string | any[]
- json:
    | string
    | number
    | boolean
    | object
    | any[] - callback: JSONPathCallback
- otherTypeCallback: JSONPathOtherTypeCallback
Returns T
+JSONPathCallable | jsonpath-plus Interface JSONPathCallable
- JSONPathCallable<T = any>(
    path: string | any[],
    json: string | number | boolean | object | any[],
    callback: JSONPathCallback,
    otherTypeCallback: JSONPathOtherTypeCallback,
): TType Parameters
Parameters
- path: string | any[]
- json: string | number | boolean | object | any[]
- callback: JSONPathCallback
- otherTypeCallback: JSONPathOtherTypeCallback
Returns T
diff --git a/docs/ts/interfaces/JSONPathOptions.html b/docs/ts/interfaces/JSONPathOptions.html
index 054718a..12680fe 100644
--- a/docs/ts/interfaces/JSONPathOptions.html
+++ b/docs/ts/interfaces/JSONPathOptions.html
@@ -1,31 +1,31 @@
-JSONPathOptions | jsonpath-plus Interface JSONPathOptions
interface JSONPathOptions {
    autostart?: boolean;
    callback?: JSONPathCallback;
    eval?:
        | boolean
        | typeof EvalClass
        | "safe"
        | "native"
        | ((code: string, context: object) => any);
    flatten?: boolean;
    ignoreEvalErrors?: boolean;
    json:
        | string
        | number
        | boolean
        | object
        | any[];
    otherTypeCallback?: JSONPathOtherTypeCallback;
    parent?: any;
    parentProperty?: any;
    path: string | any[];
    resultType?:
        | "value"
        | "path"
        | "pointer"
        | "parent"
        | "parentProperty"
        | "all";
    sandbox?: Map<string, any>;
    wrap?: boolean;
}Hierarchy (view full)
- JSONPathOptions
Index
Properties
Properties
Optionalautostart
autostart?: booleanIf this is supplied as false, one may call the evaluate method
+
JSONPathOptions | jsonpath-plus Interface JSONPathOptions
interface JSONPathOptions {
    autostart?: boolean;
    callback?: JSONPathCallback;
    eval?:
        | boolean
        | typeof EvalClass
        | "safe"
        | "native"
        | (code: string, context: object) => any;
    flatten?: boolean;
    ignoreEvalErrors?: boolean;
    json: string | number | boolean | object | any[];
    otherTypeCallback?: JSONPathOtherTypeCallback;
    parent?: any;
    parentProperty?: any;
    path: string | any[];
    resultType?:
        | "value"
        | "path"
        | "pointer"
        | "parent"
        | "parentProperty"
        | "all";
    sandbox?: Map<string, any>;
    wrap?: boolean;
}Hierarchy (View Summary)
- JSONPathOptions
Index
Properties
Properties
Optionalautostart
autostart?: booleanIf this is supplied as false, one may call the evaluate method
manually.
-Optionalcallback
If supplied, a callback will be called immediately upon retrieval of
an end point value.
The three arguments supplied will be the value of the payload
(according to resultType), the type of the payload (whether it is
a normal "value" or a "property" name), and a full payload object
(with all resultTypes).
-Optionaleval
eval?:
    | boolean
    | typeof EvalClass
    | "safe"
    | "native"
    | ((code: string, context: object) => any)Script evaluation method.
+Optionaleval
eval?:
    | boolean
    | typeof EvalClass
    | "safe"
    | "native"
    | (code: string, context: object) => anyScript evaluation method.
safe: In browser, it will use a minimal scripting engine which doesn't
use eval or Function and satisfies Content Security Policy. In NodeJS,
it has no effect and is equivalent to native as scripting is safe there.
@@ -37,23 +37,23 @@
with code and context as arguments to return the evaluated value.
class: A class similar to nodejs vm.Script. It will be created with code as constructor argument and the code
is evaluated by calling runInNewContext with context.
-Optionalflatten
flatten?: booleanWhether the returned array of results will be flattened to a
single dimension array.
-Optionalignore Eval Errors
ignoreEvalErrors?: booleanIgnore errors while evaluating JSONPath expression.
true: Don't break entire search if an error occurs while evaluating JSONPath expression on one key/value pair.
false: Break entire search if an error occurs while evaluating JSONPath expression on one key/value pair.
-json
json: string | number | boolean | object | any[]The JSON object to evaluate (whether of null, boolean, number,
string, object, or array type).
-Optionalother Type Callback
In the current absence of JSON Schema support,
+
Optionalother Type Callback
In the current absence of JSON Schema support,
one can determine types beyond the built-in types by adding the
perator @other() at the end of one's query.
If such a path is encountered, the otherTypeCallback will be invoked
@@ -61,36 +61,36 @@
property name, and it should return a boolean indicating whether the
supplied value belongs to the "other" type or not (or it may handle
transformations and return false).
-Optionalparent
parent?: anyIn the event that a query could be made to return the root node,
this allows the parent of that root node to be returned within results.
-Optionalparent Property
parentProperty?: anyIn the event that a query could be made to return the root node,
this allows the parentProperty of that root node to be returned within
results.
-path
path: string | any[]The JSONPath expression as a (normalized or unnormalized) string or
array.
-Optionalresult Type
result Type?:
    | "value"
    | "path"
    | "pointer"
    | "parent"
    | "parentProperty"
    | "all"Can be case-insensitive form of "value", "path", "pointer", "parent",
+
Optionalresult Type
resultType?: "value" | "path" | "pointer" | "parent" | "parentProperty" | "all"Can be case-insensitive form of "value", "path", "pointer", "parent",
or "parentProperty" to determine respectively whether to return
results as the values of the found items, as their absolute paths,
as JSON Pointers to the absolute paths, as their parent objects,
or as their parent's property name.
If set to "all", all of these types will be returned on an object with
the type as key name.
-Optionalsandbox
sandbox?: Map<string, any>Key-value map of variables to be available to code evaluations such
as filtering expressions.
(Note that the current path and value will also be available to those
expressions; see the Syntax section for details.)
-Optionalwrap
wrap?: booleanWhether or not to wrap the results in an array.
+Optionalwrap
wrap?: booleanWhether or not to wrap the results in an array.
If wrap is set to false, and no results are found, undefined will be
returned (as opposed to an empty array when wrap is set to true).
If wrap is set to false and a single non-array result is found, that
@@ -99,7 +99,7 @@
To avoid ambiguities (in the case where it is necessary to distinguish
between a result which is a failure and one which is an empty array),
it is recommended to switch the default to false.
-
+
diff --git a/docs/ts/interfaces/JSONPathOptionsAutoStart.html b/docs/ts/interfaces/JSONPathOptionsAutoStart.html
index 0a023c1..d3aa79d 100644
--- a/docs/ts/interfaces/JSONPathOptionsAutoStart.html
+++ b/docs/ts/interfaces/JSONPathOptionsAutoStart.html
@@ -1,31 +1,31 @@
-JSONPathOptionsAutoStart | jsonpath-plus Interface JSONPathOptionsAutoStart
interface JSONPathOptionsAutoStart {
    autostart: false;
    callback?: JSONPathCallback;
    eval?:
        | boolean
        | typeof EvalClass
        | "safe"
        | "native"
        | ((code: string, context: object) => any);
    flatten?: boolean;
    ignoreEvalErrors?: boolean;
    json:
        | string
        | number
        | boolean
        | object
        | any[];
    otherTypeCallback?: JSONPathOtherTypeCallback;
    parent?: any;
    parentProperty?: any;
    path: string | any[];
    resultType?:
        | "value"
        | "path"
        | "pointer"
        | "parent"
        | "parentProperty"
        | "all";
    sandbox?: Map<string, any>;
    wrap?: boolean;
}Hierarchy (view full)
- JSONPathOptions
- JSONPathOptionsAutoStart
Index
Properties
Properties
autostart
autostartIf this is supplied as false, one may call the evaluate method
+
JSONPathOptionsAutoStart | jsonpath-plus Interface JSONPathOptionsAutoStart
interface JSONPathOptionsAutoStart {
    autostart: false;
    callback?: JSONPathCallback;
    eval?:
        | boolean
        | typeof EvalClass
        | "safe"
        | "native"
        | (code: string, context: object) => any;
    flatten?: boolean;
    ignoreEvalErrors?: boolean;
    json: string | number | boolean | object | any[];
    otherTypeCallback?: JSONPathOtherTypeCallback;
    parent?: any;
    parentProperty?: any;
    path: string | any[];
    resultType?:
        | "value"
        | "path"
        | "pointer"
        | "parent"
        | "parentProperty"
        | "all";
    sandbox?: Map<string, any>;
    wrap?: boolean;
}Hierarchy (View Summary)
- JSONPathOptions
- JSONPathOptionsAutoStart
Index
Properties
Properties
autostart
autostart: falseIf this is supplied as false, one may call the evaluate method
manually.
-Optionalcallback
If supplied, a callback will be called immediately upon retrieval of
an end point value.
The three arguments supplied will be the value of the payload
(according to resultType), the type of the payload (whether it is
a normal "value" or a "property" name), and a full payload object
(with all resultTypes).
-Optionaleval
eval?:
    | boolean
    | typeof EvalClass
    | "safe"
    | "native"
    | ((code: string, context: object) => any)Script evaluation method.
+Optionaleval
eval?:
    | boolean
    | typeof EvalClass
    | "safe"
    | "native"
    | (code: string, context: object) => anyScript evaluation method.
safe: In browser, it will use a minimal scripting engine which doesn't
use eval or Function and satisfies Content Security Policy. In NodeJS,
it has no effect and is equivalent to native as scripting is safe there.
@@ -37,23 +37,23 @@
with code and context as arguments to return the evaluated value.
class: A class similar to nodejs vm.Script. It will be created with code as constructor argument and the code
is evaluated by calling runInNewContext with context.
-Optionalflatten
flatten?: booleanWhether the returned array of results will be flattened to a
single dimension array.
-Optionalignore Eval Errors
ignoreEvalErrors?: booleanIgnore errors while evaluating JSONPath expression.
true: Don't break entire search if an error occurs while evaluating JSONPath expression on one key/value pair.
false: Break entire search if an error occurs while evaluating JSONPath expression on one key/value pair.
-json
json: string | number | boolean | object | any[]The JSON object to evaluate (whether of null, boolean, number,
string, object, or array type).
-Optionalother Type Callback
In the current absence of JSON Schema support,
+
Optionalother Type Callback
In the current absence of JSON Schema support,
one can determine types beyond the built-in types by adding the
perator @other() at the end of one's query.
If such a path is encountered, the otherTypeCallback will be invoked
@@ -61,36 +61,36 @@
property name, and it should return a boolean indicating whether the
supplied value belongs to the "other" type or not (or it may handle
transformations and return false).
-Optionalparent
parent?: anyIn the event that a query could be made to return the root node,
this allows the parent of that root node to be returned within results.
-Optionalparent Property
parentProperty?: anyIn the event that a query could be made to return the root node,
this allows the parentProperty of that root node to be returned within
results.
-path
path: string | any[]The JSONPath expression as a (normalized or unnormalized) string or
array.
-Optionalresult Type
result Type?:
    | "value"
    | "path"
    | "pointer"
    | "parent"
    | "parentProperty"
    | "all"Can be case-insensitive form of "value", "path", "pointer", "parent",
+
Optionalresult Type
resultType?: "value" | "path" | "pointer" | "parent" | "parentProperty" | "all"Can be case-insensitive form of "value", "path", "pointer", "parent",
or "parentProperty" to determine respectively whether to return
results as the values of the found items, as their absolute paths,
as JSON Pointers to the absolute paths, as their parent objects,
or as their parent's property name.
If set to "all", all of these types will be returned on an object with
the type as key name.
-Optionalsandbox
sandbox?: Map<string, any>Key-value map of variables to be available to code evaluations such
as filtering expressions.
(Note that the current path and value will also be available to those
expressions; see the Syntax section for details.)
-Optionalwrap
wrap?: booleanWhether or not to wrap the results in an array.
+Optionalwrap
wrap?: booleanWhether or not to wrap the results in an array.
If wrap is set to false, and no results are found, undefined will be
returned (as opposed to an empty array when wrap is set to true).
If wrap is set to false and a single non-array result is found, that
@@ -99,7 +99,7 @@
To avoid ambiguities (in the case where it is necessary to distinguish
between a result which is a failure and one which is an empty array),
it is recommended to switch the default to false.
-
+
diff --git a/docs/ts/modules.html b/docs/ts/modules.html
index b2d3e63..452e7f9 100644
--- a/docs/ts/modules.html
+++ b/docs/ts/modules.html
@@ -1,10 +1 @@
-jsonpath-plus jsonpath-plus
Index
Classes
Interfaces
Type Aliases
Functions
JSONPath
-
+jsonpath-plus jsonpath-plus
Classes
Functions
diff --git a/docs/ts/types/JSONPathCallback.html b/docs/ts/types/JSONPathCallback.html
index 234a6f8..8437772 100644
--- a/docs/ts/types/JSONPathCallback.html
+++ b/docs/ts/types/JSONPathCallback.html
@@ -1 +1 @@
-JSONPathCallback | jsonpath-plus Type Alias JSONPathCallback
JSONPath Callback: ((payload: any, payloadType: any, fullPayload: any) => any)
+JSONPathCallback | jsonpath-plus Type Alias JSONPathCallback
JSONPathCallback: (payload: any, payloadType: any, fullPayload: any) => anyType declaration
- (payload: any, payloadType: any, fullPayload: any): any
Parameters
- payload: any
- payloadType: any
- fullPayload: any
Returns any
diff --git a/docs/ts/types/JSONPathOtherTypeCallback.html b/docs/ts/types/JSONPathOtherTypeCallback.html
index e737dbc..af6fdf0 100644
--- a/docs/ts/types/JSONPathOtherTypeCallback.html
+++ b/docs/ts/types/JSONPathOtherTypeCallback.html
@@ -1 +1 @@
-JSONPathOtherTypeCallback | jsonpath-plus Type Alias JSONPathOtherTypeCallback
JSONPath Other Type Callback: ((...args: any[]) => void)
+JSONPathOtherTypeCallback | jsonpath-plus Type Alias JSONPathOtherTypeCallback
JSONPathOtherTypeCallback: (...args: any[]) => voidType declaration
- (...args: any[]): void
Parameters
- ...args: any[]
Returns void
diff --git a/docs/ts/types/JSONPathType.html b/docs/ts/types/JSONPathType.html
index 6483b2c..7bdb547 100644
--- a/docs/ts/types/JSONPathType.html
+++ b/docs/ts/types/JSONPathType.html
@@ -1 +1 @@
-JSONPathType | jsonpath-plus Type Alias JSONPathType
+JSONPathType | jsonpath-plus Type Alias JSONPathType
diff --git a/src/Safe-Script.js b/src/Safe-Script.js
index 4014dc0..6ce9c4c 100644
--- a/src/Safe-Script.js
+++ b/src/Safe-Script.js
@@ -6,6 +6,7 @@ import jsepAssignment from '@jsep-plugin/assignment';
// register plugins
jsep.plugins.register(jsepRegex, jsepAssignment);
jsep.addUnaryOp('typeof');
+jsep.addUnaryOp('void');
jsep.addLiteral('null', null);
jsep.addLiteral('undefined', undefined);
@@ -147,7 +148,9 @@ const SafeEval = {
'~': (a) => ~SafeEval.evalAst(a, subs),
// eslint-disable-next-line no-implicit-coercion -- API
'+': (a) => +SafeEval.evalAst(a, subs),
- typeof: (a) => typeof SafeEval.evalAst(a, subs)
+ typeof: (a) => typeof SafeEval.evalAst(a, subs),
+ // eslint-disable-next-line no-void, sonarjs/void-use -- feature
+ void: (a) => void SafeEval.evalAst(a, subs)
}[ast.operator](ast.argument);
return result;
},
diff --git a/test/test.safe-eval.js b/test/test.safe-eval.js
index 8b2d397..9a3df13 100644
--- a/test/test.safe-eval.js
+++ b/test/test.safe-eval.js
@@ -231,7 +231,8 @@ checkBuiltInVMAndNodeVM(function (vmType, setBuiltInState) {
['!', '$..[?(@ && @.emptyArray && !@.emptyArray.length)]', [json.store.books[1]]],
['~', '$..[?(@ && ~@.shelf === -2)]', [json.store.book]],
['+ (unary)', '$..[?(@ && +@.meta === 12)]', [json.store.book]],
- ['typeof', '$..[?(@ && typeof @.meta === "number")]', [json.store.books[0]]]
+ ['typeof', '$..[?(@ && typeof @.meta === "number")]', [json.store.books[0]]],
+ ['void', '$..books[?(@.meta === void 0)]', [json.store.books[1]]]
];
for (const [operator, path, expected] of opPathExpecteds) {
it(`${operator} operator`, () => {
Exposes the cache object for those who wish to preserve and reuse +
- Preparing search index...
- The search index is not available
jsonpath-plusClass JSONPathClass
Index
Constructors
Properties
Methods
Constructors
constructor
Returns JSONPathClass
Properties
cache
Exposes the cache object for those who wish to preserve and reuse it for optimization purposes.
-Methods
evaluate
Parameters
    | string
    | number
    | boolean
    | object
    | any[]
Returns any
Parameters
    callback: JSONPathCallback;
    json:
        | string
        | number
        | boolean
        | object
        | any[];
    otherTypeCallback: JSONPathOtherTypeCallback;
    path: string | any[];
}
callback: JSONPathCallback
json:
    | string
    | number
    | boolean
    | object
    | any[]
other Type Callback: JSONPathOtherTypeCallback
path: string | any[]
Returns any
to Path Array
Accepts a normalized or unnormalized path as string and +
Methods
evaluate
    path: string | any[],
    json: string | number | boolean | object | any[],
    callback: JSONPathCallback,
    otherTypeCallback: JSONPathOtherTypeCallback,
): any
Parameters
Returns any
    options: {
        callback: JSONPathCallback;
        json: string | number | boolean | object | any[];
        otherTypeCallback: JSONPathOtherTypeCallback;
        path: string | any[];
    },
): any
Parameters
    callback: JSONPathCallback;
    json: string | number | boolean | object | any[];
    otherTypeCallback: JSONPathOtherTypeCallback;
    path: string | any[];
}
Returns any
to Path Array
Accepts a normalized or unnormalized path as string and converts to an array: for example,
-['$', 'aProperty', 'anotherProperty'].Parameters
Returns string[]
to Path String
Accepts a path array and converts to a normalized path string. +
Parameters
Returns string[]
to Path String
Accepts a path array and converts 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
Returns string
to Pointer
Accepts a path array and converts to a JSON Pointer.
+Parameters
Returns string
to Pointer
Accepts a path array and converts 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
Returns any
Settings
On This Page
Constructors
Properties
Methods
Parameters
Returns any