Sha256: 628e3338bb41c197c0dd8801a1e69e9944e40be05ca32d7af0a864ad3ac2ecf8
Contents?: true
Size: 1.06 KB
Versions: 9
Compression:
Stored size: 1.06 KB
Contents
require 'json' require 'jsonpath' module Embulk module Guess class Jsonpath < TextGuessPlugin Plugin.register_guess("jsonpath", self) def guess_text(config, sample_text) parser_config = config.param("parser",:hash) json_path = parser_config.param("root",:string,default: "$") json = JsonPath.new(json_path).on(sample_text).first if( json.kind_of?(Array) ) no_hash = json.find{ |j| !j.kind_of?(Hash) } raise RuntimeError,"Can't exec guess. The row data must be hash." if no_hash columns = Embulk::Guess::SchemaGuess.from_hash_records(json).map do |c| column = {name: c.name, type: c.type} column[:format] = c.format if c.format column end parser_guessed = {"type" => "jsonpath"} parser_guessed["columns"] = columns return {"parser" => parser_guessed} else raise RuntimeError,"Can't guess specified the JSONPath: #{json_path}. The results does not return an Array." end end end end end
Version data entries
9 entries across 9 versions & 2 rubygems