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

1.0.2 • Public • Published

jsonpath-pg

npm version ci

jsonpath-pg is a lightweight Node.js package that compiles the JSONPath parsing C code from the PostgreSQL source codebase into WebAssembly. It exposes an easy-to-use interface for converting JSONPath expressions into an Abstract Syntax Tree (AST).

Note: This package requires Node.js v16+.

Features

  • Uses PostgreSQL's JSONPath parsing code to ensure accurate and efficient parsing, notability the grammar and lexer files
  • Converts the C code into WebAssembly for performance and easy integration with Node.js
  • Provides a simple and user-friendly interface

Installation

Install the package using npm:

npm install jsonpath-pg

Usage

Import the jsonpathToAst function and use it to parse JSONPath expressions:

import { jsonpathToAst } from 'jsonpath-pg';

const expression = '$.hello.world';
const ast = jsonpathToAst(expression);

console.log(JSON.stringify(ast, null, 2));

This will output the following AST:

{
  "expr": [
    {
      "type": "$"
    },
    {
      "type": ".key",
      "value": "hello"
    },
    {
      "type": ".key",
      "value": "world"
    }
  ],
  "lax": true
}

Example 2

import { jsonpathToAst } from 'jsonpath-pg';

const expression = '$.track ? (exists(@.segments[*] ? (@.HR > 130))).segments.size()';
const ast = jsonpathToAst(expression);

console.log(JSON.stringify(ast, null, 2));
Click me: AST console output
{
  "expr": [
    {
      "type": "$"
    },
    {
      "type": ".key",
      "value": "track"
    },
    {
      "type": "?",
      "arg": [
        {
          "type": "exists",
          "arg": [
            {
              "type": "@"
            },
            {
              "type": ".key",
              "value": "segments"
            },
            {
              "type": "[*]"
            },
            {
              "type": "?",
              "arg": [
                {
                  "type": ">",
                  "left": [
                    {
                      "type": "@"
                    },
                    {
                      "type": ".key",
                      "value": "HR"
                    }
                  ],
                  "right": [
                    {
                      "type": "numeric",
                      "value": 130
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "type": ".key",
      "value": "segments"
    },
    {
      "type": "size"
    }
  ],
  "lax": true
}

API

jsonpathToAst(expression: string): object

Parses a JSONPath expression into an AST.

Arguments:

  • expression (string): The JSONPath expression to parse.

Returns:

An object representing the AST of the parsed JSONPath expression.

Errors:

If the given string cannot be parsed as a valid JSONPath expression, then throws an InvalidJsonpathExpression error.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.1-beta.12beta
1.0.21latest
0.0.1-alpha.11alpha

Version History

VersionDownloads (Last 7 Days)Published
1.0.21
1.0.12
1.0.1-beta.12
1.0.01
1.0.0-beta.21
1.0.0-beta.10
0.0.1-alpha.11
0.0.1-alpha.01

Package Sidebar

Install

npm i jsonpath-pg

Weekly Downloads

0

Version

1.0.2

License

ISC

Unpacked Size

304 kB

Total Files

7

Last publish

Collaborators

  • zone117x