Skip to content

Latest commit

 

History

History
53 lines (37 loc) · 1.12 KB

File metadata and controls

53 lines (37 loc) · 1.12 KB

json-cursor-path

coverage size

Get the JSONPath at any cursor position in JSON text. Perfect for editor plugins, JSON tools, and autocomplete features.

Try the interactive demo

demo

Getting started

  1. Install json-cursor-path

    npm i --save json-cursor-path
  2. Import JsonCursorPath using ES import

    import { JsonCursorPath } from "json-cursor-path";
  3. Create an instance and pass the raw JSON file

    const cursorPath = new JsonCursorPath(
      '{\n  "key": [\n    "val1",\n    "val2"\n  ]\n}'
    );
  4. Get the path corresponding to a cursor position

    console.log(cursorPath.get(20));
    /**
     * Output: "$.key[0]"
     */
    
    console.log(cursorPath.get(20, true));
    /**
     * Output: [
     *    { type: 'object', key: 'key' },
     *    { type: 'array', index: 0 }
     * ]
     */

Performance

O(n) (cost of traversing the JSON until the specified cursor)