igoldchluk

igoldchluk

JSONPath - RFC9535 compliant JSON Path library

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

Most Liked

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.

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

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]"}]}

Last Post!

igoldchluk

igoldchluk

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:

Where Next?

Popular in Announcing Top

tmbb
I’ve decided to create this topic to discuss optimization possibilities for something like Phoenix LiveView. I’ve created this topic unde...
144 10847 141
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
ityonemo
Currently just starting out on a new mini-project - getting zig NIFs to run in elixir. https://github.com/ityonemo/zigler The idea here...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
dominicletz
Hi, I thought I had posted my library before but seems I hadn’t. The project is still in early stages but it’s growing and so I think it...
New
kip
Image is an image processing library for Elixir. It is based upon the fabulous vix library that provides a libvips wrapper for Elixir. I...
622 19610 194
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement