https://github.com/7ph/json-cursor-path
Convert a cursor position in a JSON file to the path in the parsed JSON object
https://github.com/7ph/json-cursor-path
json json-parser json-path
Last synced: 5 months ago
JSON representation
Convert a cursor position in a JSON file to the path in the parsed JSON object
- Host: GitHub
- URL: https://github.com/7ph/json-cursor-path
- Owner: 7PH
- Created: 2024-07-01T15:45:57.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-07-08T22:57:47.000Z (almost 2 years ago)
- Last Synced: 2026-01-12T11:58:56.008Z (5 months ago)
- Topics: json, json-parser, json-path
- Language: TypeScript
- Homepage:
- Size: 282 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# json-cursor-path


Get the JSONPath at any cursor position in JSON text. Perfect for editor plugins, JSON tools, and autocomplete features.
## Getting started
1. Install `json-cursor-path`
```bash
npm i --save json-cursor-path
```
2. Import JsonCursorPath using ES import
```js
import { JsonCursorPath } from "json-cursor-path";
```
3. Create an instance and pass the raw JSON file
```js
const cursorPath = new JsonCursorPath(
'{\n "key": [\n "val1",\n "val2"\n ]\n}'
);
```
4. Get the path corresponding to a cursor position
```js
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)