npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsonpath-pg

v1.0.1

Published

Parse PostgreSQL jsonpath expressions

Downloads

10

Readme

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));
{
  "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.