Skip to content

Commit 1e5d714

Browse files
committed
- Docs (README): Indicate features, including performance (removing old note)
- Docs (README): Add headings for setup and fix headings levels - Docs (README): Indicate parent selector was not present in original spec (not just not documented) - Docs (README): Fix escaping
1 parent 99a757c commit 1e5d714

1 file changed

Lines changed: 57 additions & 21 deletions

File tree

README.md

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,59 @@
33
Analyse, transform, and selectively extract data from JSON
44
documents (and JavaScript objects).
55

6-
**Note that `jsonpath-plus` may be suffering from [performance problems](https://github.com/s3u/JSONPath/issues/14)
7-
and the maintainers are not currently able to work on resolving.
8-
You may wish to use [jsonpath](https://www.npmjs.com/package/jsonpath)
9-
to avoid this problem (though noting that it does not include the
10-
proprietary features added to `jsonpath-plus`).**
11-
12-
# Install
6+
`jsonpath-plus` expands on the original specification to add some
7+
additional operators and makes explicit some behaviors the original
8+
did not spell out.
9+
10+
## Features
11+
12+
* **Compliant** with the original jsonpath spec
13+
* Consistently **performant** per
14+
[json-querying-performance-testing](https://github.com/andykais/json-querying-performance-testing>)
15+
benchmarking, particularly for large data sets.
16+
* Convenient **additions or elaborations** not provided in the original spec:
17+
* `^` for grabbing the **parent** of a matching item
18+
* `~` for grabbing **property names** of matching items (as array)
19+
* **Type selectors** for obtaining:
20+
* Basic JSON types: `@null()`, `@boolean()`, `@number()`, `@string()`, `@array()`, `@object()`
21+
* `@integer()`
22+
* The compound type `@scalar()` (which also accepts `undefined` and
23+
non-finite numbers when querying JavaScript objects as well as all of the basic non-object/non-function types)
24+
* `@other()` usable in conjunction with a user-defined `otherTypeCallback`
25+
* Non-JSON types that can nevertheless be used when querying
26+
non-JSON JavaScript objects (`@undefined()`, `@function()`, `@nonFinite()`)
27+
* `@path`/`@parent`/`@property`/`@parentProperty` **shorthand selectors** within filters
28+
* **Escaping**
29+
* `` ` `` for escaping remaining sequence
30+
* `@['...']`/`?@['...']` syntax for escaping special characters within
31+
property names in filters
32+
* Documents `$..` (**getting all parent components**)
33+
* **ESM** and **UMD** export formats
34+
* In addition to queried values, **can return various meta-information**
35+
including paths or pointers to the value, as well as the parent
36+
object and parent property name (to allow for modification).
37+
* **Utilities for converting** between paths, arrays, and pointers
38+
* Option to **prevent evaluations** permitted in the original spec or supply
39+
a **sandbox** for evaluated values.
40+
* Option for **callback to handle results** as they are obtained.
41+
42+
## Install
1343

1444
```shell
1545
npm install jsonpath-plus
1646
```
1747

18-
# Usage
19-
20-
## Syntax
48+
## Setup
2149

22-
In Node.js:
50+
### Node.js
2351

2452
```js
2553
const {JSONPath} = require('jsonpath-plus');
2654
const result = JSONPath({path: '...', json: ...});
2755
```
2856

57+
### Browser
58+
2959
For browser usage you can directly include `dist/index-umd.js`; no Browserify
3060
magic is necessary:
3161

@@ -36,6 +66,8 @@ const result = JSONPath({path: '...', json: ...});
3666
</script>
3767
```
3868

69+
### ESM (Modern browsers)
70+
3971
You may also use ES6 Module imports (for modern browsers):
4072

4173
```html
@@ -45,13 +77,17 @@ const result = JSONPath({path: '...', json: ...});
4577
</script>
4678
```
4779

80+
### ESM (Bundlers)
81+
4882
Or if you are bundling your JavaScript (e.g., with Rollup), just use:
4983

5084
```js
5185
import JSONPath from 'jsonpath-plus';
5286
const result = JSONPath({path: '...', json: ...});
5387
```
5488

89+
## Usage
90+
5591
The full signature available is:
5692

5793
```js
@@ -69,7 +105,7 @@ the callback function being executed 0 to N times depending
69105
on the number of independent items to be found in the result.
70106
See the docs below for more on `JSONPath`'s available arguments.
71107

72-
## Properties
108+
### Properties
73109

74110
The properties that can be supplied on the options object or
75111
evaluate method (as the first argument) include:
@@ -129,7 +165,7 @@ evaluate method (as the first argument) include:
129165
belongs to the "other" type or not (or it may handle transformations and
130166
return false).
131167

132-
## Instance methods
168+
### Instance methods
133169

134170
- ***evaluate(path, json, callback, otherTypeCallback)*** OR
135171
***evaluate({path: \<path\>, json: \<json object\>, callback:***
@@ -141,7 +177,7 @@ evaluate method (as the first argument) include:
141177
accept any of the other allowed instance properties (except
142178
for `autostart` which would have no relevance here).
143179

144-
## Class properties and methods
180+
### Class properties and methods
145181

146182
- ***JSONPath.cache*** - Exposes the cache object for those who wish
147183
to preserve and reuse it for optimization purposes.
@@ -160,7 +196,7 @@ evaluate method (as the first argument) include:
160196
Pointer spec). The JSONPath terminal constructions `~` and `^` and
161197
type operators like `@string()` are silently stripped.
162198

163-
# Syntax through examples
199+
## Syntax through examples
164200

165201
Given the following JSON, taken from <http://goessner.net/articles/JsonPath/>:
166202

@@ -260,7 +296,7 @@ comparisons or to prevent ambiguity).
260296
/ | $ | 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
261297
//\*/\*\|//\*/\*/text() | $..* | All Elements (and text) beneath root in an XML document. All members of a JSON structure beneath the root. |
262298
//* | $.. | 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
263-
//*\[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 documented in the original spec
299+
//*\[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
264300
/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
265301
/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
266302
//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
@@ -269,13 +305,13 @@ comparisons or to prevent ambiguity).
269305
/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
270306
//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
271307
//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
272-
| | `` ` `` (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
308+
| | `` ` `` (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
273309

274310
Any additional variables supplied as properties on the optional "sandbox"
275311
object option are also available to (parenthetical-based)
276312
evaluations.
277313

278-
# Potential sources of confusion for XPath users
314+
## Potential sources of confusion for XPath users
279315

280316
1. In JSONPath, a filter expression, in addition to its `@` being a
281317
reference to its children, actually selects the immediate children
@@ -286,14 +322,14 @@ from 0), whereas in XPath, they are 1-based.
286322
1. In JSONPath, equality tests utilize (as per JavaScript) multiple equal signs
287323
whereas in XPath, they use a single equal sign.
288324

289-
# Ideas
325+
## Ideas
290326

291327
1. Support OR outside of filters (as in XPath `|`) and grouping.
292328
1. Create syntax to work like XPath filters in not selecting children?
293329
1. Allow option for parentNode equivalent (maintaining entire chain of
294330
parent-and-parentProperty objects up to root)
295331

296-
# Development
332+
## Development
297333

298334
Running the tests on Node:
299335

@@ -311,6 +347,6 @@ npm run browser-test
311347

312348
- Visit [http://localhost:8082/test/](http://localhost:8082/test/).
313349

314-
# License
350+
## License
315351

316352
[MIT License](http://www.opensource.org/licenses/mit-license.php).

0 commit comments

Comments
 (0)