JSONPathEx.Parser (JSONPathEx v0.3.0)

Copy Markdown View Source

Defines parsers for JSONPath expressions using NimbleParsec.

Handles JSONPath syntax including filters, grouping, and the main JSONPath expression. The parsed output is structured and tagged for further processing.

Summary

Functions

Parses an expression with logical and arithmetic operators.

Parses a filter expression (e.g., [?(@.price < 10)]).

Parses a grouping expression, optionally negated.

Parses a JSONPath expression.

Parses a JSONPath string and returns the parsed result or an error.

Functions

expression(binary, opts \\ [])

@spec expression(binary(), keyword()) ::
  {:ok, [term()], rest, context, line, byte_offset}
  | {:error, reason, rest, context, line, byte_offset}
when line: {pos_integer(), byte_offset},
     byte_offset: non_neg_integer(),
     rest: binary(),
     reason: String.t(),
     context: map()

Parses an expression with logical and arithmetic operators.

filter_expression(binary, opts \\ [])

@spec filter_expression(binary(), keyword()) ::
  {:ok, [term()], rest, context, line, byte_offset}
  | {:error, reason, rest, context, line, byte_offset}
when line: {pos_integer(), byte_offset},
     byte_offset: non_neg_integer(),
     rest: binary(),
     reason: String.t(),
     context: map()

Parses a filter expression (e.g., [?(@.price < 10)]).

Supports both [?(expr)] and shorthand [?expr].

grouping(binary, opts \\ [])

@spec grouping(binary(), keyword()) ::
  {:ok, [term()], rest, context, line, byte_offset}
  | {:error, reason, rest, context, line, byte_offset}
when line: {pos_integer(), byte_offset},
     byte_offset: non_neg_integer(),
     rest: binary(),
     reason: String.t(),
     context: map()

Parses a grouping expression, optionally negated.

jsonpath(binary, opts \\ [])

@spec jsonpath(binary(), keyword()) ::
  {:ok, [term()], rest, context, line, byte_offset}
  | {:error, reason, rest, context, line, byte_offset}
when line: {pos_integer(), byte_offset},
     byte_offset: non_neg_integer(),
     rest: binary(),
     reason: String.t(),
     context: map()

Parses a JSONPath expression.

Returns a tagged structure on success or an error tuple on failure.

parse(value)

Parses a JSONPath string and returns the parsed result or an error.

Examples

iex> {:ok, ast} = JSONPathEx.Parser.parse("$.store.book[*].author")
iex> List.first(ast)
{:root, "$"}

iex> {:error, msg} = JSONPathEx.Parser.parse("invalid")
iex> String.contains?(msg, "expected")
true