Sha256: 4eb55b511d04775b22acfccb4785f6376d568d3ee3df6f08b239db2d43faf9e8

Contents?: true

Size: 796 Bytes

Versions: 1

Compression:

Stored size: 796 Bytes

Contents

module JsonHelper
    def resolve_json_path(json, template)
      if template.start_with?("$") then
        JsonPath.on(json, template)
      else
        paths = template.scan(/(\{(.*?)\})/).uniq
        return template if paths.empty?
        combinations = paths.map { |path| {path[0] => JsonPath.on(json, path[1])} }.reduce(&:merge)

        combinations = combinations.values[0].product(*combinations.values[1..-1]).map{ |x| combinations.keys.zip(x).to_h }

        combinations.map do |c|
          t = template+''
          c.keys.each do |key|
            t.gsub!(key, c[key])
          end
          t
        end
      end
    end

    def valid_json?(json)
      begin
        JSON.parse(json)
        return true
      rescue Exception => e
        return false
      end
    end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chaoite-0.1.4 lib/chaoite/json_helper.rb