Read and write values to locations referenced by JSON pointers as described in RFC 6901.
# If you're using npm
npm install @zakgolba/jsonptr --save
# If you're using yarn
yarn add @zakgolba/jsonptr
Attempts to write a value to the location that the pointer
references.
If the value is stored successfully, the cached pointers for the target
provided will be invalidated.
Returns a boolean
that represents whether or not the value was able to be written to the location that the pointer
references.
import jsonptr from "@zakgolba/jsonptr";
const data = { hello: {} };
jsonptr.assign(data, "/hello/world", "Hello, world!");
console.log(data.hello.world);
// => Hello, world!
console.log(jsonptr.assign(data, "/invalid/reference", "Hello, world!"));
// => false
Compiles and returns a JSON pointer from an iterable of unescaped tokens.
import jsonptr from "@zakgolba/jsonptr";
console.log(jsonptr.compile(["hello", "world"]));
// => /hello/world
console.log(jsonptr.compile(["hello/world", "pointer"]));
// => /hello~1world/pointer
Attempts to read and return the value at the location that the pointer
references.
import jsonptr from "@zakgolba/jsonptr";
const data = { hello: { world: "Hello, world!" } };
console.log(jsonptr.deref(data, "/hello/world"));
// => Hello, world!
console.log(jsonptr.deref(data, "/this/points/nowhere"));
// => undefined
Lazily parses and unescapes each token in the provided pointer
.
import jsonptr from "@zakgolba/jsonptr";
console.log([...jsonptr.parse("/hello/world")]);
// => ["hello", "world"]
for (const token of jsonptr.parse("/hello~1world/pointer")) {
console.log(token);
}
// => hello/world
// => pointer
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.