@winglet/json
Version:
TypeScript library for safe and efficient JSON data manipulation with RFC 6901 (JSON Pointer) and RFC 6902 (JSON Patch) compliance, featuring prototype pollution protection and immutable operations
20 lines (19 loc) • 751 B
TypeScript
/**
* Generates a JSONPath from the root object to the target object.
* JSONPath is a query language for JSON similar to XPath for XML.
*
* @template Root - Root object type
* @template Target - Target object type
* @param root - Root object to start the search from
* @param target - Target object to find
* @returns JSONPath string to the target object or null if not found
*
* @example
* const obj = { a: { b: [1, 2, { c: 'found' }] } };
* getJSONPath(obj, obj.a.b[2]); // '$.a.b[2]'
*
* @example
* const obj = { 'key.with.dots': { value: 'found' } };
* getJSONPath(obj, obj['key.with.dots']); // "$['key.with.dots']"
*/
export declare const getJSONPath: <Root extends object, Target>(root: Root, target: Target) => string | null;