or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

change-observation.mdcore-operations.mdindex.mdjson-pointer-utilities.mdvalidation.md
tile.json

json-pointer-utilities.mddocs/

JSON Pointer Utilities

Helper functions for working with JSON pointers, path manipulation, and deep cloning that are part of the public fast-json-patch API.

Capabilities

JSON Pointer Operations

Get Value by Pointer

Retrieves a value from a JSON document using JSON Pointer notation (RFC-6901).

/**
 * Retrieves a value from a JSON document by a JSON pointer.
 * Returns the value.
 * @param document The document to get the value from
 * @param pointer An escaped JSON pointer
 * @returns The retrieved value
 */
function getValueByPointer(document: any, pointer: string): any;

Usage Examples:

import { getValueByPointer } from "fast-json-patch";

const document = {
  users: [
    { 
      name: "Alice", 
      contact: { 
        email: "alice@example.com",
        phone: "555-0123" 
      }
    },
    { 
      name: "Bob",
      contact: {
        email: "bob@example.com" 
      }
    }
  ],
  metadata: {
    version: "1.0",
    "special/key": "value with slash",
    "tilde~key": "value with tilde"
  }
};

// Basic pointer usage
const firstUser = getValueByPointer(document, "/users/0");
// { name: "Alice", contact: { email: "alice@example.com", phone: "555-0123" } }

// Deep nested access
const email = getValueByPointer(document, "/users/0/contact/email");
// "alice@example.com"

// Array index access
const secondUserName = getValueByPointer(document, "/users/1/name");
// "Bob"

// Root document access
const entireDoc = getValueByPointer(document, "");
// Returns the entire document

// Special characters in keys (escaped)
const specialValue = getValueByPointer(document, "/metadata/special~1key");
// "value with slash"

const tildeValue = getValueByPointer(document, "/metadata/tilde~0key");
// "value with tilde"

Escape Path Component

Escapes special characters in JSON pointer path components according to RFC-6901.

/**
 * Escapes a json pointer path component
 * @param path The raw pointer component
 * @returns The escaped path component
 */
function escapePathComponent(path: string): string;

Usage Examples:

import { escapePathComponent } from "fast-json-patch";

// Escape forward slashes and tildes
const escaped1 = escapePathComponent("user/profile");  // "user~1profile"
const escaped2 = escapePathComponent("tilde~value");   // "tilde~0value"
const escaped3 = escapePathComponent("both/and~");     // "both~1and~0"

// No escaping needed
const escaped4 = escapePathComponent("normalkey");     // "normalkey"

// Use in path construction
const userKey = "user/admin";
const fullPath = "/" + escapePathComponent(userKey);   // "/user~1admin"

Unescape Path Component

Unescapes JSON pointer path components, converting escape sequences back to original characters.

/**
 * Unescapes a json pointer path component
 * @param path The escaped pointer component
 * @returns The unescaped path component
 */
function unescapePathComponent(path: string): string;

Usage Examples:

import { unescapePathComponent } from "fast-json-patch";

// Unescape sequences
const unescaped1 = unescapePathComponent("user~1profile");  // "user/profile"
const unescaped2 = unescapePathComponent("tilde~0value");   // "tilde~value"
const unescaped3 = unescapePathComponent("both~1and~0");    // "both/and~"

// No unescaping needed
const unescaped4 = unescapePathComponent("normalkey");      // "normalkey"

Deep Cloning

Deep Clone Objects

Creates deep copies of objects and primitives, handling various JavaScript types.

/**
 * Deeply clone the object.
 * @param obj Value to clone
 * @returns Cloned object
 */
function deepClone(obj: any): any;

Usage Examples:

import { deepClone } from "fast-json-patch";

const original = {
  name: "Alice",
  hobbies: ["reading", "swimming"],
  profile: {
    age: 25,
    preferences: {
      theme: "dark",
      notifications: true
    }
  }
};

// Deep clone the object
const cloned = deepClone(original);

// Modify the clone
cloned.name = "Bob";
cloned.hobbies.push("cycling");
cloned.profile.age = 30;

// Original remains unchanged
console.log(original.name);        // "Alice"
console.log(original.hobbies);     // ["reading", "swimming"]
console.log(original.profile.age); // 25

// Handle special values
const specialCloned = deepClone(undefined); // null (JSON behavior)
const primitiveCloned = deepClone(42);      // 42
const stringCloned = deepClone("hello");    // "hello"

JSON Pointer Specification

These utilities implement the JSON Pointer specification (RFC-6901):

  • Empty string ("") refers to the root document
  • Forward slash (/) separates path segments
  • Tilde escape sequences:
    • ~0 represents ~ (tilde)
    • ~1 represents / (forward slash)
  • Array indices are represented as decimal strings ("0", "1", etc.)
  • Special character handling ensures safe path traversal

Performance Notes

  • deepClone uses JSON.parse/stringify for optimal performance on plain objects
  • isInteger uses character code checking instead of regex for speed
  • Path operations are optimized for common use cases
  • Functions handle edge cases and provide consistent behavior across environments

These utilities provide the foundation for robust JSON Patch operations while maintaining compatibility with web standards and ensuring safe object manipulation.