Class: Embulk::Guess::Jsonpath

Inherits:
TextGuessPlugin
  • Object
show all
Defined in:
lib/embulk/guess/jsonpath.rb

Instance Method Summary collapse

Instance Method Details

#guess_text(config, sample_text) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/embulk/guess/jsonpath.rb', line 9

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