json-cursor-path
v1.0.1
Published
Convert a position in a raw JSON to the path in the parsed object
Downloads
45
Maintainers
Readme
json-cursor-path
Convert a cursor position in a JSON file to the path in the parsed JSON object.
Getting started
Install
json-cursor-path
npm i --save json-cursor-path
Import JsonCursorPath using ES import
import { JsonCursorPath } from "json-cursor-path";
Create an instance and pass the raw JSON file
const cursorPath = new JsonCursorPath( '{\n "key": [\n "val1",\n "val2"\n ]\n}' );
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)