json8-pointer
Version:
JSON Pointer toolkit for JavaScript
274 lines (183 loc) • 4.84 kB
Markdown
JSON Pointer [RFC 6901](http://tools.ietf.org/html/rfc6901) toolkit for JavaScript.
See also [JSON8 Patch](https://github.com/sonnyp/JSON8/tree/main/packages/patch) for more methods to work with JSON pointers.
---
- [Introduction](
- [Getting started](
- [Methods](
- [find](
- [context](
- [encode](
- [serialize](
- [escape](
- [decode](
- [parse](
- [unescape](
- [join](
- [index](
- [dict](
- [flatten](
- [unflatten](
- [compile](
`npm install json8-pointer`
---
```javascript
const pointer = require("json8-pointer");
```
[↑](
Use a JSON Pointer to find a value in a JSON document.
Returns `undefined` if the value cannot be found.
```javascript
var doc = { foo: { bar: "foobar" } };
pointer.find(doc, "/foo/bar");
// "foobar"
pointer.find(doc, "/bar/foo");
// undefined
```
[↑](
Returns the target parent and target property of a pointer.
```javascript
var doc = { foo: { bar: "foobar" } };
pointer.context(doc, "/foo/bar");
// ['bar', doc.foo]
```
[↑](
Takes an array of unescaped tokens (see [decode](#decode)) and return a JSON Pointer string.
```javascript
pointer.encode(["foo", "bar", "hello"]);
// '/foo/bar/hello'
pointer.encode(["foo", "a/b"]);
// '/foo/a~1b'
```
You can specify a different separator than the default `/`.
```javascript
pointer.encode(["foo", "bar", "hello"], ".");
// '.foo.bar.hello'
```
[↑](
Alias for the [encode](
[↑](
Escape a single token for use in JSON Pointer.
```javascript
pointer.escape("a/b");
// 'a~1b'
```
You can specify a different separator than the default `/`.
```javascript
pointer.escape("a.b", ".");
// 'a~1b'
```
[↑](
Takes a JSON Pointer string and return an array of unescaped tokens.
```javascript
pointer.decode("/foo/bar/hello");
// ['foo', 'bar', 'hello'];
pointer.decode("/foo/a~1b");
// ['foo', 'a/b']
```
You can specify a different separator than the default `/`.
```javascript
pointer.decode(".foo.bar.hello", ".");
// ['foo', 'bar', 'hello'];
```
[↑](
`decode` will throw with an error if [prototype pollution](https://github.com/HoLyVieR/prototype-pollution-nsec18) is attempted.
[↑](
Alias for the [decode](
[↑](
Unescape a single token see [escape](
```javascript
pointer.unescape("a~1b");
// 'a/b'
```
You can specify a different separator than the default `/`.
```javascript
pointer.unescape("a~1b", ".");
// 'a/b'
```
[↑](
Join a base pointer and tokens;
```javascript
pointer.join("/foo", ["bar"]);
pointer.join(["foo"], "bar");
pointer.join("", ["foo", "bar"]);
pointer.join([], ["foo", "bar"]);
// `/foo/bar`
```
You can specify a different separator than the default `/`.
```javascript
pointer.join("/foo", ["bar"], ".");
// `.foo.bar`
```
[↑](
[](https://json8.github.io/pointer/demos/index/)
The `index` method returns an object with all values indexed by pointers.
```javascript
pointer.index("foo"); // {'': 'foo'}
pointer.index(["hello", "earth"]);
// {
// '': ['hello', 'earth'],
// '/0': 'hello',
// '/1': 'earth'
// }
```
[↑](
[](https://json8.github.io/pointer/demos/index/)
Just like [index](
```javascript
pointer.dict(['hello', 'earth'])
// {
// '/0': 'hello',
// '/1': 'earth'
// }
pointer.dict({'foo', 'bar'})
// {
// '/foo': 'bar'
// }
```
[↑](
[](https://json8.github.io/pointer/demos/flatten/)
The `flatten` method works like a flat version of [index](
```javascript
pointer.flatten(["hello", "earth"]);
// {
// '': [],
// '/0': 'hello',
// '/1': 'earth'
// }
```
[↑](
[](https://json8.github.io/pointer/demos/flatten/)
The `unflatten` method takes an [flattened](
```javascript
pointer.unflatten({ "": "foo" }); // 'foo'
pointer.unflatten({
"": [],
"/0": "hello",
"/1": "earth",
}); // ['hello', 'earth']
```
The `compile` method takes a pointer and returns a function that accept a document and returns the value at the location of the pointer.
```javascript
const getAge = pointer.compile("/age");
getAge({ age: 22 }); // 22
```
[↑](