Gathering detailed insights and metrics for jsonpath-plus
Gathering detailed insights and metrics for jsonpath-plus
Gathering detailed insights and metrics for jsonpath-plus
Gathering detailed insights and metrics for jsonpath-plus
@types/jsonpath-plus
TypeScript definitions for jsonpath-plus
@alell/jsonpath-plus-q
JSONPath-Plus for multiple formats json, yaml and XML
@jsware/jsonpath-cli
Command-Line Interface for the jsonpath-plus library
jsonpath-plus-browser
Browser-only JS implementation of JSONPath with some additional operators
A fork of JSONPath from http://goessner.net/articles/JsonPath/
npm install jsonpath-plus
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
989 Stars
452 Commits
176 Forks
21 Watching
2 Branches
33 Contributors
Updated on 27 Nov 2024
Minified
Minified + Gzipped
JavaScript (98.66%)
HTML (1.34%)
Cumulative downloads
Total Downloads
Last day
-11.2%
914,025
Compared to previous day
Last week
2.8%
4,963,528
Compared to previous week
Last month
7.9%
21,109,360
Compared to previous month
Last year
54.2%
195,054,198
Compared to previous year
3
(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).
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()
)@path
/@parent
/@property
/@parentProperty
/@root
shorthand selectors within filters`
for escaping remaining sequence@['...']
/?@['...']
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.
1npm install jsonpath-plus
1const {JSONPath} = require('jsonpath-plus'); 2 3const result = JSONPath({path: '...', json});
For browser usage you can directly include dist/index-browser-umd.cjs
; no
Browserify magic is necessary:
1<script src="node_modules/jsonpath-plus/dist/index-browser-umd.cjs"></script> 2 3<script> 4 5const result = JSONPath.JSONPath({path: '...', json: {}}); 6 7</script>
You may also use ES6 Module imports (for modern browsers):
1<script type="module"> 2 3import { 4 JSONPath 5} from './node_modules/jsonpath-plus/dist/index-browser-esm.js'; 6 7const result = JSONPath({path: '...', json: {}}); 8 9</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):
1import {JSONPath} from 'jsonpath-plus'; 2 3const 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
.
Note that result
will contain all items found (optionally
wrapped into an array) whereas callback
can be used if you
wish to perform some operation as each item is discovered, with
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.
The properties that can be supplied on the options object or evaluate method (as the first argument) include:
false
,
one may call the evaluate
method manually.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 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
.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. false
: Disable JavaScript
evaluation expressions and throw exceptions when these expressions are attempted.
callback [ (code, context) => value]
: A custom implementation which is called
with code
and context
as arguments to return the evaluated value.
class
: A class which is created with code
as constructor argument and code
is evaluated by calling runInNewContext
with context
.
``parentProperty
of that root node to be returned within results.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
path is encountered, the otherTypeCallback
will be invoked 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).autostart
property is set to false
. It
can be used for repeated evaluations using the same configuration.
Besides the listed properties, the latter method pattern can
accept any of the other allowed instance properties (except
for autostart
which would have no relevance here).['$', 'aProperty', 'anotherProperty']
.$['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/:
1{ 2"store": { 3 "book": [ 4 { 5 "category": "reference", 6 "author": "Nigel Rees", 7 "title": "Sayings of the Century", 8 "price": 8.95 9 }, 10 { 11 "category": "fiction", 12 "author": "Evelyn Waugh", 13 "title": "Sword of Honour", 14 "price": 12.99 15 }, 16 { 17 "category": "fiction", 18 "author": "Herman Melville", 19 "title": "Moby Dick", 20 "isbn": "0-553-21311-3", 21 "price": 8.99 22 }, 23 { 24 "category": "fiction", 25 "author": "J. R. R. Tolkien", 26 "title": "The Lord of the Rings", 27 "isbn": "0-395-19395-8", 28 "price": 22.99 29 } 30 ], 31 "bicycle": { 32 "color": "red", 33 "price": 19.95 34 } 35} 36}
and the following XML representation:
1<store> 2 <book> 3 <category>reference</category> 4 <author>Nigel Rees</author> 5 <title>Sayings of the Century</title> 6 <price>8.95</price> 7 </book> 8 <book> 9 <category>fiction</category> 10 <author>Evelyn Waugh</author> 11 <title>Sword of Honour</title> 12 <price>12.99</price> 13 </book> 14 <book> 15 <category>fiction</category> 16 <author>Herman Melville</author> 17 <title>Moby Dick</title> 18 <isbn>0-553-21311-3</isbn> 19 <price>8.99</price> 20 </book> 21 <book> 22 <category>fiction</category> 23 <author>J. R. R. Tolkien</author> 24 <title>The Lord of the Rings</title> 25 <isbn>0-395-19395-8</isbn> 26 <price>22.99</price> 27 </book> 28 <bicycle> 29 <color>red</color> 30 <price>19.95</price> 31 </bicycle> 32</store>
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
may be helpful (set to xml
or xml-strict
).
XPath | JSONPath | Result | Notes |
---|---|---|---|
/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 | All authors | |
/store/* | $.store.* | All things in store, which are its books (a book array) and a red bicycle (a bicycle object). | |
/store//price | $.store..price | The price of everything in the store. | |
//book[3] | $..book[2] | The third book (book object) | |
//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] | The first two books | |
//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)] | 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)] | Filter all books cheaper than 10 | |
//*[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() | $..* | 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)]^ | 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.*~ | 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]")] | All books besides that at the path pointing to the first | @path not present in the original spec |
//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 |
//book/*[name() != 'category'] | $..book.*[?(@property !== "category")] | Grabs all children of "book" except for "category" ones | @property is not present in the original spec |
//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 |
/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 |
//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 |
//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 |
//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 |
//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. |
//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. |
` (e.g., `$ to match a property literally named $ ) | 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
but delimit which of its parent nodes will be obtained in the result.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:
1npm test
For in-browser tests:
1npm run browser-test
The latest stable version of the package.
Stable Version
1
9.8/10
Summary
JSONPath Plus Remote Code Execution (RCE) Vulnerability
Affected Versions
< 10.0.7
Patched Versions
10.0.7
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
security policy file detected
Details
Reason
23 commit(s) and 5 issue activity found in the last 90 days -- score normalized to 10
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 5/25 approved changesets -- score normalized to 2
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More