CILicenseVersionDocs

JSONPath

RFC-9535 compliant JSONPath query evaluator for Elixir, with 100% passing rate for the JSONPath Compliance Test Suite

Installation

Add json_path to your list of dependencies in mix.exs:

def deps do
  [
    {:json_path, "~> 0.1.0"}
  ]
end

Usage

JSONPath supports string keys only.

You can pass the JSONPath query expression as a string

root = [%{"a" => %{"b" => "c"}}, %{"b" => %{"a" => 1}}]
query = "$..a"
JSONPath.evaluate(root, query)
# {:ok, [%{"b" => "c"}, 1]}

Or if you plan use the same query multiple times, you can build the expression once for better performance

{:ok, query} = JSONPath.build("$[1]")
JSONPath.evaluate([], query)
#{:ok, []}
JSONPath.evaluate(["a", "b", "c"], query)
# {:ok, ["b"]}

Notes