|
| 1 | +# jsonpath-rfc9535 |
| 2 | + |
| 3 | +A JSONPath implementation based on [RFC 9535](https://datatracker.ietf.org/doc/rfc9535/). |
| 4 | + |
| 5 | +Works in any environment complying with ECMAScript 2022. |
| 6 | + |
| 7 | +It passes the entire [JSONPath Compliance Test Suite](https://github.com/jsonpath-standard/jsonpath-compliance-test-suite) with 100% success rate (tested against commit 9277705). |
| 8 | + |
| 9 | +If you are interested in a legacy specification, please check out my other package [nimma](https://github.com/P0lip/nimma). |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +To install the package, you can use npm: |
| 14 | + |
| 15 | +```sh |
| 16 | +npm install jsonpath-rfc9535 |
| 17 | +``` |
| 18 | + |
| 19 | +or a package manager of your choice. |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +```js |
| 24 | +import { query } from 'jsonpath-rfc9535'; |
| 25 | + |
| 26 | +const document = { |
| 27 | + "store": { |
| 28 | + "book": [ |
| 29 | + { |
| 30 | + "category": "reference", |
| 31 | + "author": "Nigel Rees", |
| 32 | + "title": "Sayings of the Century", |
| 33 | + "price": 8.95, |
| 34 | + }, |
| 35 | + { |
| 36 | + "category": "fiction", |
| 37 | + "author": "Evelyn Waugh", |
| 38 | + "title": "Sword of Honour", |
| 39 | + "price": 12.99, |
| 40 | + }, |
| 41 | + { |
| 42 | + "category": "fiction", |
| 43 | + "author": "Herman Melville", |
| 44 | + "title": "Moby Dick", |
| 45 | + "isbn": "0-553-21311-3", |
| 46 | + "price": 8.99, |
| 47 | + }, |
| 48 | + { |
| 49 | + "category": "fiction", |
| 50 | + "author": "J. R. R. Tolkien", |
| 51 | + "title": "The Lord of the Rings", |
| 52 | + "isbn": "0-395-19395-8", |
| 53 | + "price": 22.99, |
| 54 | + }, |
| 55 | + ], |
| 56 | + "bicycle": { |
| 57 | + "color": "red", |
| 58 | + "price": 399, |
| 59 | + }, |
| 60 | + }, |
| 61 | +}; |
| 62 | + |
| 63 | +const path = '$.store.book[*].author'; |
| 64 | +const result = query(document, path); |
| 65 | + |
| 66 | +console.log(result); // Outputs: ['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien'] |
| 67 | +``` |
| 68 | + |
| 69 | +You can also import `exec` member that takes a callback. |
| 70 | + |
| 71 | +```ts |
| 72 | +import { exec } from 'jsonpath-rfc9535'; |
| 73 | + |
| 74 | +const document = { |
| 75 | + // ... |
| 76 | +} |
| 77 | + |
| 78 | +const path = '$.store.book[*].author'; |
| 79 | +const result = exec(document, path, (value) => { |
| 80 | + // consume matched value |
| 81 | +}); |
| 82 | +``` |
| 83 | +## Parser |
| 84 | + |
| 85 | +The parser is generated using [Peggy](https://peggyjs.org/). In the future, I would like to merge it with the Nimma's [parser](https://github.com/P0lip/nimma/blob/master/src/parser/parser.mjs) to shrink down the package size as well as improve the performance. |
| 86 | + |
| 87 | +The parser can be imported as a standalone module: |
| 88 | + |
| 89 | +```js |
| 90 | +import parse from 'jsonpath-rfc9535/parser'; |
| 91 | + |
| 92 | +const ast = parse('$.store.book[*].author'); |
| 93 | +// consume ast |
| 94 | +``` |
| 95 | + |
| 96 | +## Regular Expressions |
| 97 | + |
| 98 | +Bear in mind that `match` and `search` functions accept regular expressions in the form of I-Regexp ([RFC 9485](https://datatracker.ietf.org/doc/rfc9485/)). |
| 99 | +These regular expressions are largely compatible with the standard ECMAScript regular expressions, but there is one relatively minor difference. |
| 100 | +In I-Regexp, the dot (.) character matches any character except for `\n` and `\r`. |
| 101 | +On top of that, Unicode is enabled by default. |
| 102 | + |
| 103 | +## Custom Function Extensions |
| 104 | + |
| 105 | +At the moment, custom function extensions are not supported. |
| 106 | +However, the API is designed to support them, so they might be added in the future if there is a demand for them. |
0 commit comments