jsonpath-ts
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

JSONPath Typescript

Type inference for JSONPath queries, as per the JSONPath query expressions RFC (see limitations below).

npm i jsonpath-ts

Usage

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 Selectors, 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:

import {
	ParsePath, // Input path -> ParsedPath
	ExtractValue // ParsedPath -> Return data type
} from "jsonpath-ts

Readme

Keywords

none

Package Sidebar

Install

npm i jsonpath-ts

Weekly Downloads

171

Version

0.1.1

License

none

Unpacked Size

17.9 kB

Total Files

7

Last publish

Collaborators

  • sinclairnick