https://github.com/sinclairnick/jsonpath-ts
Type inference for JSONPath queries
https://github.com/sinclairnick/jsonpath-ts
jsonpath typescript
Last synced: 6 months ago
JSON representation
Type inference for JSONPath queries
- Host: GitHub
- URL: https://github.com/sinclairnick/jsonpath-ts
- Owner: sinclairnick
- License: mit
- Created: 2024-03-22T09:09:00.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-22T22:34:41.000Z (about 2 years ago)
- Last Synced: 2025-06-04T21:33:40.018Z (10 months ago)
- Topics: jsonpath, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/jsonpath-ts
- Size: 68.4 KB
- Stars: 23
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JSONPath Typescript

Type inference for JSONPath queries, as per [RFC 9535: JSONPath: Query Expressions for JSON](https://www.rfc-editor.org/rfc/rfc9535.txt) (see limitations below).
```
npm i jsonpath-ts
```
## Usage
```ts
import { Parse } from "jsonpath-ts";
type Result = Parse<"$.store.book[*].author[2:6]", Data>;
// ^? ["Herman Melville", "J. R. R. Tolkien"]
// Given input type
export type Data = {
store: {
book: [
{
category: "reference";
author: "Nigel Rees";
title: "Sayings of the Century";
price: 8.95;
},
{
category: "fiction";
author: "Evelyn Waugh";
title: "Sword of Honour";
price: 12.99;
},
{
category: "fiction";
author: "Herman Melville";
title: "Moby Dick";
isbn: "0-553-21311-3";
price: 8.99;
},
{
category: "fiction";
author: "J. R. R. Tolkien";
title: "The Lord of the Rings";
isbn: "0-395-19395-8";
price: 22.99;
}
];
bicycle: {
color: "red";
price: 399;
};
};
};
```
## Limitations
- Slice steps not supported (`::-1`)
- Filtering not supported (`?@.hasField`)
- Descendant segments not supported (`$...X`)
## How does it work?
This implementation first converts the input path into an abstract, shallow ordered tree of `Selector`s, such as `NameSelector`, `IndexSelector` etc. This representation is then iterated over to retrieve the derive the correct type from the data.
This functionality can be imported independently via:
```ts
import {
ParsePath, // Input path -> ParsedPath
ExtractValue // ParsedPath -> Return data type
} from "jsonpath-ts
```