JSON Pointer
@hyperjump/json-pointer is an implementation of
RFC 6901 JSON Pointer. A JSON
Pointer is a string like /foo/0/bar that identifies one value in a JSON
document. This package gets, sets, and removes values by pointer. It also
builds and splits pointers, so you never have to escape them by hand.
The following example gets a value, and then sets a new one:
import { pointerGet, pointerSet } from "@hyperjump/json-pointer";
const doc = { foo: { bar: 42 } };
pointerGet("/foo/bar", doc); // 42
const updated = pointerSet("/foo/bar", doc, 33);updated.foo.bar; // 33doc.foo.bar; // 42The package offers the following:
- Immutable or in place.
pointerSetandpointerUnsetreturn a new value and leave the original alone.pointerAssignandpointerRemovechange it in place. - Parse once, apply many times. Give a function only a pointer, and it returns a function that you can apply to any number of documents.
- Escaping handled.
pointerAppend,pointerParse, andpointerStringifyescape and unescape~and/for you. Every function accepts segments as well as pointers. - Small. No dependencies. It has TypeScript types and works in Node.js and browsers.
@hyperjump/json-schema
and
@hyperjump/browser
build on it.
If you use version 1.x, see the 1.x README. For what changed in v2, see Migrate from v1.
Contents
Section titled “Contents”- Install: setup for Node.js, browsers, and TypeScript
- Pointers: how to write and build pointers, and pointers in URIs
- Get values:
pointerGet, and what it returns when a value isn’t there - Change values:
pointerSet,pointerAssign,pointerUnset, andpointerRemove - Migrate from v1: what changed, and what to do about it