Module: Philiprehberger::JsonPath
- Defined in:
- lib/philiprehberger/json_path.rb,
lib/philiprehberger/json_path/version.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
'0.5.0'
Class Method Summary collapse
-
.count(data, path) ⇒ Integer
Return the number of matches for a JSONPath expression.
-
.exists?(data, path) ⇒ Boolean
Check if a JSONPath expression matches anything.
-
.first(data, path) ⇒ Object?
Query data and return the first match.
-
.last(data, path) ⇒ Object?
Query data and return the last match.
-
.paths(data, path) ⇒ Array<String>
Return the canonical JSONPath strings for every match of an expression.
-
.query(data, path) ⇒ Array
Query data with a JSONPath expression and return all matches.
-
.update(data, path) {|value| ... } ⇒ Hash, Array
Apply a transformation to every JSONPath match in ‘data`.
-
.values(data, path) ⇒ Array
Alias for query (more discoverable name).
Class Method Details
.count(data, path) ⇒ Integer
Return the number of matches for a JSONPath expression
51 52 53 |
# File 'lib/philiprehberger/json_path.rb', line 51 def self.count(data, path) query(data, path).size end |
.exists?(data, path) ⇒ Boolean
Check if a JSONPath expression matches anything
60 61 62 |
# File 'lib/philiprehberger/json_path.rb', line 60 def self.exists?(data, path) !query(data, path).empty? end |
.first(data, path) ⇒ Object?
Query data and return the first match
33 34 35 |
# File 'lib/philiprehberger/json_path.rb', line 33 def self.first(data, path) query(data, path).first end |
.last(data, path) ⇒ Object?
Query data and return the last match
42 43 44 |
# File 'lib/philiprehberger/json_path.rb', line 42 def self.last(data, path) query(data, path).last end |
.paths(data, path) ⇒ Array<String>
Return the canonical JSONPath strings for every match of an expression
Each returned path is an unambiguous JSONPath that, when re-evaluated, resolves to the single element it identifies. Paths are returned in document order. Returns an empty array when no matches exist.
73 74 75 76 |
# File 'lib/philiprehberger/json_path.rb', line 73 def self.paths(data, path) tokens = tokenize(path) evaluate_with_paths(data, tokens).map { |_node, p| p } end |
.query(data, path) ⇒ Array
Query data with a JSONPath expression and return all matches
14 15 16 17 |
# File 'lib/philiprehberger/json_path.rb', line 14 def self.query(data, path) tokens = tokenize(path) evaluate(data, tokens) end |
.update(data, path) {|value| ... } ⇒ Hash, Array
Apply a transformation to every JSONPath match in ‘data`.
Mutates ‘data` in place and returns it for chaining. Each match is replaced with the block’s return value. Skips matches that resolve to the document root (‘“$”`). Use `Marshal.load(Marshal.dump(data))` first if you need to preserve the original.
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/philiprehberger/json_path.rb', line 91 def self.update(data, path, &block) raise Error, 'block required for update' unless block paths(data, path).each do |canonical| raise Error, 'Cannot update root document' if canonical == '$' mutate_at(data, canonical, &block) end data end |
.values(data, path) ⇒ Array
Alias for query (more discoverable name)
24 25 26 |
# File 'lib/philiprehberger/json_path.rb', line 24 def self.values(data, path) query(data, path) end |