JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 12539624
  • Score
    100M100P100Q245776F
  • License Apache-2.0

High-performance JSON Pointer implementation

Package Exports

  • @jsonjoy.com/json-pointer
  • @jsonjoy.com/json-pointer/lib/codegen/find
  • @jsonjoy.com/json-pointer/lib/codegen/find.js
  • @jsonjoy.com/json-pointer/lib/codegen/findRef
  • @jsonjoy.com/json-pointer/lib/codegen/findRef.js
  • @jsonjoy.com/json-pointer/lib/find
  • @jsonjoy.com/json-pointer/lib/find.js
  • @jsonjoy.com/json-pointer/lib/findByPointer/v6
  • @jsonjoy.com/json-pointer/lib/findByPointer/v6.js
  • @jsonjoy.com/json-pointer/lib/get
  • @jsonjoy.com/json-pointer/lib/get.js
  • @jsonjoy.com/json-pointer/lib/index.js
  • @jsonjoy.com/json-pointer/lib/util
  • @jsonjoy.com/json-pointer/lib/util.js
  • @jsonjoy.com/json-pointer/lib/validate
  • @jsonjoy.com/json-pointer/lib/validate.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@jsonjoy.com/json-pointer) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

JSON Pointer - json-pointer

Fast implementation of JSON Pointer (RFC 6901) specification in TypeScript.

Usage

Can find a value in a JSON object using three methods: (1) JSON Pointer string, (2) array of steps, or (3) a pre-compiled function.

Examples

Find the value in a JSON document at some specific location.

Find by JSON Pointer string

import { findByPointer } from '@jsonjoy.com/json-pointer';

const doc = {
  foo: {
    bar: 123,
  },
};

const res = findByPointer(doc, '/foo/bar');

Find by path array

Alternatively, you can specify an array of steps, such as ['foo', 'bar']. Or, use the parseJsonPointer function to convert a JSON Pointer string to an array.

import { find, parseJsonPointer } from '@jsonjoy.com/json-pointer';

const doc = {
  foo: {
    bar: 123,
  },
};

const path = parseJsonPointer('/foo/bar');
const ref = find(doc, path);

console.log(ref);
// { val: 123, obj: { bar: 123 }, key: 'bar' }

Pre-compiled function

If you know the path in advance, you can compile a function that will find the value at that location, it will work few times faster than the previous methods.

import { $$find } from '@jsonjoy.com/json-pointer/lib/codegen';

const doc = {
  foo: {
    bar: 123,
  },
};
const finder = $$find(['foo', 'bar']);

const res = finder(doc);

Low-level API

Convert JSON Pointer to path array and back.

import { parseJsonPointer } from '@jsonjoy.com/json-pointer';

console.log(parseJsonPointer('/f~0o~1o/bar/1/baz'));
// [ 'f~o/o', 'bar', '1', 'baz' ]

console.log(formatJsonPointer(['f~o/o', 'bar', '1', 'baz']));
// /f~0o~1o/bar/1/baz

Decode and encode a single step of JSON Pointer.

console.log(unescapeComponent('~0~1'));
// ~/

console.log(escapeComponent('~/'));
// ~0~1