Skip to content

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:

TypeScript
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; // 33
doc.foo.bar; // 42

The package offers the following:

  • Immutable or in place. pointerSet and pointerUnset return a new value and leave the original alone. pointerAssign and pointerRemove change 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, and pointerStringify escape 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.

  • 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, and pointerRemove
  • Migrate from v1: what changed, and what to do about it