| 1 | # JSON Pointer for Node.js
|
|---|
| 2 |
|
|---|
| 3 | This is an implementation of [JSON Pointer](https://tools.ietf.org/html/rfc6901).
|
|---|
| 4 |
|
|---|
| 5 | ## CLI
|
|---|
| 6 |
|
|---|
| 7 | Looking to filter JSON from the command line? Check out [jsonpointer-cli](https://github.com/joeyespo/jsonpointer-cli).
|
|---|
| 8 |
|
|---|
| 9 | ## Usage
|
|---|
| 10 | ```javascript
|
|---|
| 11 | var jsonpointer = require('jsonpointer');
|
|---|
| 12 | var obj = { foo: 1, bar: { baz: 2}, qux: [3, 4, 5]};
|
|---|
| 13 |
|
|---|
| 14 | jsonpointer.get(obj, '/foo'); // returns 1
|
|---|
| 15 | jsonpointer.get(obj, '/bar/baz'); // returns 2
|
|---|
| 16 | jsonpointer.get(obj, '/qux/0'); // returns 3
|
|---|
| 17 | jsonpointer.get(obj, '/qux/1'); // returns 4
|
|---|
| 18 | jsonpointer.get(obj, '/qux/2'); // returns 5
|
|---|
| 19 | jsonpointer.get(obj, '/quo'); // returns undefined
|
|---|
| 20 |
|
|---|
| 21 | jsonpointer.set(obj, '/foo', 6); // sets obj.foo = 6;
|
|---|
| 22 | jsonpointer.set(obj, '/qux/-', 6) // sets obj.qux = [3, 4, 5, 6]
|
|---|
| 23 |
|
|---|
| 24 | var pointer = jsonpointer.compile('/foo')
|
|---|
| 25 | pointer.get(obj) // returns 1
|
|---|
| 26 | pointer.set(obj, 1) // sets obj.foo = 1
|
|---|
| 27 | ```
|
|---|
| 28 |
|
|---|
| 29 | ## Testing
|
|---|
| 30 |
|
|---|
| 31 | $ npm test
|
|---|
| 32 | All tests pass.
|
|---|
| 33 | $
|
|---|
| 34 |
|
|---|
| 35 | [](https://github.com/janl/node-jsonpointer/actions/workflows/node.js.yml)
|
|---|
| 36 |
|
|---|
| 37 | ## Author
|
|---|
| 38 |
|
|---|
| 39 | (c) 2011-2021 Jan Lehnardt <jan@apache.org> & Marc Bachmann <https://github.com/marcbachmann>
|
|---|
| 40 |
|
|---|
| 41 | Thanks to all contributors.
|
|---|
| 42 |
|
|---|
| 43 | ## License
|
|---|
| 44 |
|
|---|
| 45 | MIT License.
|
|---|