Json Pointer
This json-pointer implementation conforms to RFC 6901, currently with one
exception:
The URI Fragment Identifier Representation #, e.g. '#/path/to/value' is treated as syntactic sugar, mainly for
readability purposes, to clarify that the given string is a json-pointer used with gson-pointer. The identifier will
be stripped when resolving values of the data and will not escape values through percent-encoding.
The URI Fragment Identifier Representation may be omitted, e.g. using #/path/to/value
is equal to /path/to/value
.
But you should be aware that using methods like join('/path/to', 'value')
will return the pointer as #/path/to/value
As the error handling is not further specified in RFC 6901, this implementation
will return undefined
for any invalid pointer/missing data, making it very handy to check uncertain data, i.e.
if (pointer.get({}, "#/path/to/nested/item") !== undefined) {
}
install
npm i gson-pointer --save
pointer.get
var data = {
parent: {
child: {
title: "title of child"
}
}
}
var titleOfChild = pointer.get(data, "#/parent/child/title");
pointer.set
set values on an object
var data = {
parent: {
children: [
{
title: "title of child"
}
]
}
};
pointer.set(data, "#/parent/children/1", {title: "second child"});
console.log(data.parent.children.length);
pointer.delete
delete properties or array items
pointer.delete(data, "#/parent/arrayOrObject/1");
Helpers
pointer.join
pointer.join
joins all arguments to a valid json pointer:
var key = "child";
var target = pointer.join("parent", key, "title");