Exposes the cache object for those who wish to preserve and reuse +it for optimization purposes.
+Accepts a path array and converts to a normalized path string.
The string will be in a form like:
- $['aProperty']['anotherProperty][0]
.
+$['aProperty']['anotherProperty][0]
.
The JSONPath terminal constructions ~
and ^
and type operators
- like @string()
are silently stripped.
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.
@string()
are silently stripped.
+Exposes the cache object for those who wish to preserve and reuse - it for optimization purposes.
-Exposes the cache object for those who wish to preserve and reuse +it for optimization purposes.
+Accepts a path array and converts to a normalized path string.
The string will be in a form like:
- $['aProperty']['anotherProperty][0]
.
+$['aProperty']['anotherProperty][0]
.
The JSONPath terminal constructions ~
and ^
and type operators
- like @string()
are silently stripped.
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.
@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.
-^
for grabbing the parent of a matching item~
for grabbing property names of matching items (as array)@null()
, @boolean()
, @number()
, @string()
, @array()
, @object()
@integer()
@scalar()
(which also accepts undefined
and
- non-finite numbers when querying JavaScript objects as well as all of the basic non-object/non-function types)@other()
usable in conjunction with a user-defined otherTypeCallback
@undefined()
, @function()
, @nonFinite()
)@undefined()
, @function()
, @nonFinite()
)@path
/@parent
/@property
/@parentProperty
/@root
shorthand selectors within filters`
for escaping remaining sequence@['...']
/?@['...']
syntax for escaping special characters within
- property names in filters@['...']
/?@['...']
syntax for escaping special characters within
+property names in filters$..
(getting all parent components)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
-
-const {JSONPath} = require('jsonpath-plus');
const result = JSONPath({path: '...', json});
-
-For browser usage you can directly include dist/index-browser-umd.cjs
; no
+
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
+
+
+const {JSONPath} = require('jsonpath-plus');
const result = JSONPath({path: '...', json});
+
+
+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>
-
-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>
-
-Or if you are bundling your JavaScript (e.g., with Rollup), just use,
-noting that mainFields
+
<script src="node_modules/jsonpath-plus/dist/index-browser-umd.cjs"></script>
<script>
const result = JSONPath.JSONPath({path: '...', json: {}});
</script>
+
+
+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>
+
+
+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});
-
-The full signature available is:
+import {JSONPath} from 'jsonpath-plus';
const result = JSONPath({path: '...', json});
+
+
+The full signature available is:
const result = JSONPath([options,] path, json, callback, otherTypeCallback);
+
The arguments path
, json
, callback
, and otherTypeCallback
can alternatively be expressed (along with any other of the
available properties) on options
.
JSONPath
's available arguments.
-See also the API docs.
-The properties that can be supplied on the options object or
+See the docs below for more on JSONPath
's available arguments.
See also the API docs.
+The properties that can be supplied on the options object or evaluate method (as the first argument) include:
false
.safe
: In browser, it will use a minimal scripting engine which doesn't
+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.
native
: uses the native scripting capabilities. i.e. unsafe eval
or
@@ -138,17 +147,17 @@
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 resultType
s).@other()
at the end of one's query. If such a
+adding the operator @other()
at the end of one's query. If such a
path is encountered, the otherTypeCallback
will be invoked with the
-value of the item, its path, its parent, and its parent's property name,
+value of the item, its path, its parent, and its parent's 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).autostart
which would have no relevance here).['$', 'aProperty', 'anotherProperty']
.['$', 'aProperty', 'anotherProperty']
.
$['aProperty']['anotherProperty][0]
. The JSONPath terminal
+like: $['aProperty']['anotherProperty][0]
. The JSONPath terminal
constructions ~
and ^
and type operators like @string()
are
silently stripped./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.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
}
}
}
-
+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
}
}
}
+
+
and the following XML representation:
-<store>
<book>
<category>reference</category>
<author>Nigel Rees</author>
<title>Sayings of the Century</title>
<price>8.95</price>
</book>
<book>
<category>fiction</category>
<author>Evelyn Waugh</author>
<title>Sword of Honour</title>
<price>12.99</price>
</book>
<book>
<category>fiction</category>
<author>Herman Melville</author>
<title>Moby Dick</title>
<isbn>0-553-21311-3</isbn>
<price>8.99</price>
</book>
<book>
<category>fiction</category>
<author>J. R. R. Tolkien</author>
<title>The Lord of the Rings</title>
<isbn>0-395-19395-8</isbn>
<price>22.99</price>
</book>
<bicycle>
<color>red</color>
<price>19.95</price>
</bicycle>
</store>
-
+
+
+ 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
+
+
+
+
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
).
Notes | -|||||||
---|---|---|---|---|---|---|---|
/store/book/author | $.store.book[*].author | The authors of all books in the store | @@ -231,7 +274,7 @@//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) |
+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] | @@ -249,7 +292,7 @@//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) |
+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] | @@ -258,8 +301,8 @@|||||||
//*[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 |
||
/store/book[not(. is /store/book[1])] (in XPath 2.0) | -$.store.book[?(@path !== "$['store']['book'][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 | @parent is not present in the original spec | ||
//book/*[name() != 'category'] | +//book/*[name() != 'category'] | $..book.*[?(@property !== "category")] | Grabs all children of "book" except for "category" ones | @property is not present in the original spec | @@ -318,9 +361,9 @@@property is not present in the original spec | ||
/store/*/*[name(parent::*) != '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") | +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 | ||
@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/*[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) | +All categories of books which match the regex (end in 'TION' case insensitive) | @property is not present in the original spec. | ||
//book/[matches(name(), 'bn$')]/parent:: (XPath 2.0) | +//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) | +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. |
||
Escapes the entire sequence following (to be treated as a literal) | ` is not present in the original spec; to get a literal backtick, use an additional backtick to escape |
Any additional variables supplied as properties on the optional "sandbox" object option are also available to (parenthetical-based) evaluations.
-@
being a
reference to its children, actually selects the immediate children
as well, whereas in XPath, filter conditions do not select the children
@@ -373,24 +417,26 @@
A basic command line interface (CLI) is provided. Access it using npx jsonpath-plus <json-file> <jsonpath-query>
.
A basic command line interface (CLI) is provided. Access it using npx jsonpath-plus <json-file> <jsonpath-query>
.
|
) and grouping.Running the tests on Node:
-npm test
-
+Running the tests on Node:
+npm test
+
+
For in-browser tests:
npm run browser-test
-
+npm run browser-test
+
+
Optional
autostartIf this is supplied as false, one may call the evaluate method - manually.
-true
-
-Optional
callbackIf supplied, a callback will be called immediately upon retrieval of +
Optional
autostartIf this is supplied as false, one may call the evaluate method +manually.
+Optional
callbackIf 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 resultType
s).
undefined
-
-Optional
evalScript evaluation method.
-safe
: In browser, it will use a minimal scripting engine which doesn't
+
Optional
evalScript 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.
native
: uses the native scripting capabilities. i.e. unsafe eval
or
Function
in browser and vm.Script
in nodejs.
true
: Same as 'safe'
true
: Same as 'safe'
false
: Disable Javascript executions in path string. Same as preventEval: true
in previous versions.
callback [ (code, context) => value]
: A custom implementation which is called
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
.
'safe'
-
-Optional
flattenWhether the returned array of results will be flattened to a - single dimension array.
-false
-
-Optional
ignoreIgnore errors while evaluating JSONPath expression.
-true
: Don't break entire search if an error occurs while evaluating JSONPath expression on one key/value pair.
Optional
flattenWhether the returned array of results will be flattened to a +single dimension array.
+Optional
ignoreIgnore 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.
false
-
-The JSON object to evaluate (whether of null, boolean, number, - string, object, or array type).
-Optional
otherIn the current absence of JSON Schema support, +
The JSON object to evaluate (whether of null, boolean, number, +string, object, or array type).
+Optional
otherIn 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.
@other()
at the end of one's query.
If such a path is encountered, the otherTypeCallback
will be invoked
-with the value of the item, its path, its parent, and its parent's
+with the value of the item, its path, its parent, and its parent's
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).
undefined
- <A function that throws an error when @other()
is encountered>
Optional
parentIn the event that a query could be made to return the root node, +
Optional
parentIn 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.
-null
-
-Optional
parentIn the event that a query could be made to return the root node, +
Optional
parentIn 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.
-null
-
-The JSONPath expression as a (normalized or unnormalized) string or - array.
-Optional
resultCan 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.
+The JSONPath expression as a (normalized or unnormalized) string or +array.
+Optional
resultCan 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.
-'value'
-
-Optional
sandboxKey-value map of variables to be available to code evaluations such - as filtering expressions. +the type as key name.
+Optional
sandboxKey-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.)
-Optional
wrapWhether or not to wrap the results in an array.
+expressions; see the Syntax section for details.) +Optional
wrapWhether 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).
+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 - result will be the only item returned (not within an array).
+result will be the only item returned (not within an array).An array will still be returned if multiple results are found, however. 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.
-true
-
-If this is supplied as false, one may call the evaluate method - manually.
-true
-
-Optional
callbackIf supplied, a callback will be called immediately upon retrieval of +
If this is supplied as false, one may call the evaluate method +manually.
+Optional
callbackIf 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 resultType
s).
undefined
-
-Optional
evalScript evaluation method.
-safe
: In browser, it will use a minimal scripting engine which doesn't
+
Optional
evalScript 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.
native
: uses the native scripting capabilities. i.e. unsafe eval
or
Function
in browser and vm.Script
in nodejs.
true
: Same as 'safe'
true
: Same as 'safe'
false
: Disable Javascript executions in path string. Same as preventEval: true
in previous versions.
callback [ (code, context) => value]
: A custom implementation which is called
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
.
'safe'
-
-Optional
flattenWhether the returned array of results will be flattened to a - single dimension array.
-false
-
-Optional
ignoreIgnore errors while evaluating JSONPath expression.
-true
: Don't break entire search if an error occurs while evaluating JSONPath expression on one key/value pair.
Optional
flattenWhether the returned array of results will be flattened to a +single dimension array.
+Optional
ignoreIgnore 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.
false
-
-The JSON object to evaluate (whether of null, boolean, number, - string, object, or array type).
-Optional
otherIn the current absence of JSON Schema support, +
The JSON object to evaluate (whether of null, boolean, number, +string, object, or array type).
+Optional
otherIn 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.
@other()
at the end of one's query.
If such a path is encountered, the otherTypeCallback
will be invoked
-with the value of the item, its path, its parent, and its parent's
+with the value of the item, its path, its parent, and its parent's
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).
undefined
- <A function that throws an error when @other()
is encountered>
Optional
parentIn the event that a query could be made to return the root node, +
Optional
parentIn 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.
-null
-
-Optional
parentIn the event that a query could be made to return the root node, +
Optional
parentIn 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.
-null
-
-The JSONPath expression as a (normalized or unnormalized) string or - array.
-Optional
resultCan 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.
+The JSONPath expression as a (normalized or unnormalized) string or +array.
+Optional
resultCan 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.
-'value'
-
-Optional
sandboxKey-value map of variables to be available to code evaluations such - as filtering expressions. +the type as key name.
+Optional
sandboxKey-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.)
-Optional
wrapWhether or not to wrap the results in an array.
+expressions; see the Syntax section for details.) +Optional
wrapWhether 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).
+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 - result will be the only item returned (not within an array).
+result will be the only item returned (not within an array).An array will still be returned if multiple results are found, however. 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.
-true
-
-Rest
...args: any[]
Exposes the cache object for those who wish to preserve and reuse - it for optimization purposes.
-