Module: Janeway

Defined in:
lib/janeway.rb,
lib/janeway/ast.rb,
lib/janeway/error.rb,
lib/janeway/lexer.rb,
lib/janeway/query.rb,
lib/janeway/token.rb,
lib/janeway/parser.rb,
lib/janeway/version.rb,
lib/janeway/ast/null.rb,
lib/janeway/ast/error.rb,
lib/janeway/functions.rb,
lib/janeway/ast/number.rb,
lib/janeway/enumerator.rb,
lib/janeway/ast/boolean.rb,
lib/janeway/ast/helpers.rb,
lib/janeway/interpreter.rb,
lib/janeway/ast/function.rb,
lib/janeway/ast/selector.rb,
lib/janeway/ast/root_node.rb,
lib/janeway/ast/expression.rb,
lib/janeway/ast/string_type.rb,
lib/janeway/functions/count.rb,
lib/janeway/functions/match.rb,
lib/janeway/functions/value.rb,
lib/janeway/normalized_path.rb,
lib/janeway/ast/current_node.rb,
lib/janeway/functions/length.rb,
lib/janeway/functions/search.rb,
lib/janeway/ast/child_segment.rb,
lib/janeway/ast/name_selector.rb,
lib/janeway/interpreters/base.rb,
lib/janeway/ast/index_selector.rb,
lib/janeway/ast/unary_operator.rb,
lib/janeway/ast/binary_operator.rb,
lib/janeway/ast/filter_selector.rb,
lib/janeway/interpreters/yielder.rb,
lib/janeway/ast/wildcard_selector.rb,
lib/janeway/ast/descendant_segment.rb,
lib/janeway/ast/array_slice_selector.rb,
lib/janeway/interpreters/iteration_helper.rb,
lib/janeway/interpreters/tree_constructor.rb,
lib/janeway/interpreters/root_node_deleter.rb,
lib/janeway/interpreters/root_node_delete_if.rb,
lib/janeway/interpreters/function_interpreter.rb,
lib/janeway/interpreters/child_segment_deleter.rb,
lib/janeway/interpreters/name_selector_deleter.rb,
lib/janeway/interpreters/root_node_interpreter.rb,
lib/janeway/interpreters/index_selector_deleter.rb,
lib/janeway/interpreters/child_segment_delete_if.rb,
lib/janeway/interpreters/filter_selector_deleter.rb,
lib/janeway/interpreters/name_selector_delete_if.rb,
lib/janeway/interpreters/current_node_interpreter.rb,
lib/janeway/interpreters/index_selector_delete_if.rb,
lib/janeway/interpreters/child_segment_interpreter.rb,
lib/janeway/interpreters/filter_selector_delete_if.rb,
lib/janeway/interpreters/name_selector_interpreter.rb,
lib/janeway/interpreters/wildcard_selector_deleter.rb,
lib/janeway/interpreters/index_selector_interpreter.rb,
lib/janeway/interpreters/unary_operator_interpreter.rb,
lib/janeway/interpreters/binary_operator_interpreter.rb,
lib/janeway/interpreters/filter_selector_interpreter.rb,
lib/janeway/interpreters/wildcard_selector_delete_if.rb,
lib/janeway/interpreters/array_slice_selector_deleter.rb,
lib/janeway/interpreters/wildcard_selector_interpreter.rb,
lib/janeway/interpreters/array_slice_selector_delete_if.rb,
lib/janeway/interpreters/descendant_segment_interpreter.rb,
lib/janeway/interpreters/array_slice_selector_interpreter.rb

Overview

Janeway JSONPath query library

github.com/gongfarmer/janeway

Defined Under Namespace

Modules: AST, Functions, Interpreters, NormalizedPath Classes: Enumerator, Error, Interpreter, Lexer, Parser, Query, Token

Constant Summary collapse

VERSION =

Version for janeway-jsonpath gem

'0.6.0'

Class Method Summary collapse

Class Method Details

.enum_for(jsonpath, data) ⇒ Janeway::Enumerator

Parse a jsonpath string and combine it with data to make an Enumerator.

The Enumerator can be used to apply the query to the data using Enumerator module methods such as #each and #map.

Examples:

Apply query to data and search to get array of results

results = Janeway.parse('$.store.books[? length(@.title) > 20]').search

Apply query to data and iterate over results

enum = Janeway.parse('$.store.books[? length(@.title) > 20]')
enum.each do |book|
  results << book
end

Parameters:

  • jsonpath (String)

    jsonpath query

  • data (Array, Hash)

    input data

Returns:

See Also:



30
31
32
33
# File 'lib/janeway.rb', line 30

def self.enum_for(jsonpath, data)
  query = parse(jsonpath)
  Janeway::Enumerator.new(query, data)
end

.parse(query) ⇒ Janeway::AST::Query

Parse a JSONPath string into a Janeway::Query object.

This object can be combined with data to create Enumerators that apply the query to the data.

Use this method if you want to parse the query once and re-use it for multiple data sets.

Otherwise, use Janeway.enum_for to parse the query and pair it with data in a single step.

Examples:

Use a query to search several JSON files

results = []
query = Janeway.parse('$.store.books[? length(@.title) > 20]')
data_files.each do |path|
  data = JSON.parse File.read(path)
  results.concat query.enum_for(data).search
end

Parameters:

  • query (String)

    jsonpath query

Returns:

  • (Janeway::AST::Query)


53
54
55
# File 'lib/janeway.rb', line 53

def self.parse(query)
  Janeway::Parser.parse(query)
end

.path_to_diggable(jsonpath) ⇒ Array<String, Integer>

Transform a jsonpath singular query into an array of values suitable for providing to Hash#dig or Array#dig.

Only singular queries are allowed, meaning queries that contain only name selectors (ie. hash keys) and index selectors (array indexes.) The paths that are yielded to Enumerator#each are all suitable for this.

Examples:

convert normalized jsonpath to array of hash keys / array indices

Janeway.path_to_diggable('$["a"].b.c[0]') => ["a", "b", "c", 0]

Parameters:

  • jsonpath (String)

    jsonpath query

Returns:

  • (Array<String, Integer>)

Raises:

  • (NotImplementedError)


69
70
71
# File 'lib/janeway.rb', line 69

def self.path_to_diggable(jsonpath)
  raise NotImplementedError
end