igoldchluk

igoldchluk

Hi everyone

I needed a way to evaluate arbitrary JSON Path expressions, and I could not find any existing library that was fully complaint with RFC-9535. The two most popular libraries I found (ExJsonPath and Warpath) predate the RFC and, while sufficient for most common cases, they still contained some incompatible behavior, so I decided to implement a new one from scratch.

Introducing JSONPath, an RFC-9535 compliant JSON Path query evaluator.

Features

  • Passes 100% of the JSONPath Compliance Test Suite.
  • Supports all function extensions: length, count, match, search, value.
  • No external dependencies.
  • Does not support additional function extensions, for example sum, common in pre RFC9535 implementations.
  • Does not support atom keys.

The only discrepancy with RFC9535 is regarding regular expressions. For match and search, the standard expects I-RegExp style expressions, while in JSONPath library any valid Elixir regular expression are accepted.

If you need any of the non-standarised functions/behaviors I would recommend to use any of the pre-RFC libraries. If you need to follow the standard as strictly as possible then consider giving JSONPath a try :grinning_face:

Basic usage

document = [
      %{"name" => "Alice", "age" => 20},
      %{"name" => "Bob", "age" => 30},
      %{"name" => "Charlie", "age" => 25}
    ]
query = "$[?@.age < 27].name"
JSONPath.evaluate(document, query)
# {:ok, ["Alice", "Charlie"]}

Links

Showing Posts 1 to 5

krasenyp

krasenyp

Congratulations on the release. Do you plan on implementing the ability to validate JSON Path expressions at compile time?

Edit:
I ask because there are PostgreSQL functions which work with JSON Path and it’d be cool to write Ecto helpers which can, at compile time, tell you “hey, this path is wrong”.

igoldchluk

igoldchluk OP

Thank you

The code already checks that the argument is a valid JSON Path expression before doing any evaluation, and returns an error when the expression is invalid. I forgot to include examples both in the README and in the announcement post :sweat_smile: You can find some examples in the project’s README now for invalid expressions. I also added bang functions in case you want to do @query JSONPath.build!(“…”) .

As for SQL integration, unfortunately the syntax is different for SQL/JSON compared to RFC-9535. For example the query "$[?@.age < 27].name” from the example becomes something like "$[*] ? (@.age < 27).name”. My use case is RFC-9535 expressions exclusively, I have no plans at the moment of doing anything with SQL/JSON queries + Ecto.

igoldchluk

igoldchluk OP

New version 0.3 released

JSONPath.evaluate/3 now takes an optional 3rd argument to specify the returned type. Possible values are:

  • :values - Returns the node values, same as previous versions
  • :paths - Returns the normalized paths
  • :values_and_paths - Returns a list of 2-element tuples {node_value, normalized_path}

Examples:

root = %{"people" => [%{"name" => "Alice", "age" => 20}, %{"name" => "Bob", "age" => 30}]}
query = "$.people[?@.age > 25]"
JSONPath.evaluate(root, query, :values)
# {:ok, [%{"name" => "Bob", "age" => 30}]}

JSONPath.evaluate(root, query, :paths)
# {:ok, ["$['people'][1]"]}

JSONPath.evaluate(root, query, :values_and_paths)
#{:ok, [{%{"name" => "Bob", "age" => 30}, "$['people'][1]"}]}
krasenyp

krasenyp

That’s a great feature but I have a question. Do you think encoding the different result formats in a single struct makes sense? Changing the return type through a flag argument is a smell in my opinion.

igoldchluk

igoldchluk OP

Yes I wasn’t fully convinced either. I might change it to 3 separate functions instead in a future version. I also take suggestions if you have a better idea, before I make another questionable decision :grin:

— All posts loaded —

Where Next? Top

Trending in Announcing Top

wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 11030 135
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New
woylie
Phoenix components for pagination, sortable tables and filter forms with Flop and (optionally) Ecto. pagination cursor pagination sorta...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mudasobwa
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews