class JsonPaths
Constants
- VERSION
Attributes
json[R]
paths[R]
Public Class Methods
new(json)
click to toggle source
# File lib/json_paths.rb, line 10 def initialize(json) @json = json @paths = json_to_paths end
Public Instance Methods
each()
click to toggle source
# File lib/json_paths.rb, line 16 def each paths.each end
json_to_paths()
click to toggle source
# File lib/json_paths.rb, line 20 def json_to_paths parsed_json = JSON.parse(json) paths = [] parsed_json.each_key do |key| paths << key_to_path(parsed_json, key) end paths.flatten.map { |path| "$." + path } end
key_to_path(hash, key)
click to toggle source
# File lib/json_paths.rb, line 31 def key_to_path(hash, key) paths_for_key = [] value = hash[key] if value.respond_to?(:keys) value.each_key do |k| paths_for_key << key_to_path(value, k).map do |ktp| ktp = ktp.first unless ktp.is_a?(String) key + "." + ktp end end elsif value.respond_to?(:each_with_index) value.each_with_index do |v,i| paths_for_key << JsonPaths.new(v.to_json).paths.map do |j| "#{key}[#{i}].#{j.gsub('$.', '')}" end end else paths_for_key << key.to_s end paths_for_key end