Class: Daru::IO::Importers::JSON

Inherits:
Base
  • Object
show all
Defined in:
lib/daru/io/importers/json.rb

Overview

JSON Importer Class, that extends from_json and read_json methods to Daru::DataFrame

Direct Known Subclasses

Mongo

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

guess_parse

Methods inherited from Base

#optional_gem

Constructor Details

#initializeJSON

Checks for required gem dependencies of JSON Importer



13
14
15
16
17
# File 'lib/daru/io/importers/json.rb', line 13

def initialize
  require 'open-uri'
  require 'json'
  optional_gem 'jsonpath'
end

Class Method Details

.from(instance) ⇒ Daru::IO::Importers::JSON

Loads from a Ruby structure of Hashes and / or Arrays

Examples:

Loading from Ruby Hash of Arrays

from_instance = Daru::IO::Importers::JSON.from({x: [1,4], y: [2,5], z: [3, 6]})

Parameters:

  • instance (Hash or Array)

    A simple / complexly nested JSON structure

Returns:



51
52
53
54
55
# File 'lib/daru/io/importers/json.rb', line 51

def from(instance)
  @file_data = instance
  @json      = @file_data.is_a?(String) ? ::JSON.parse(@file_data) : @file_data
  self
end

.read(path) ⇒ Daru::IO::Importers::JSON

Reads data from a json file / remote json response

Examples:

Reading from simply nested remote json response

url = 'https://data.nasa.gov/resource/2vr3-k9wn.json'
simple_read_instance = Daru::IO::Importers::JSON.read(url)

Reading from complexy nested remote json response

url = 'http://api.tvmaze.com/singlesearch/shows?q=game-of-thrones&embed=episodes'
complex_read_instance = Daru::IO::Importers::JSON.read(url)

Parameters:

  • path (String)

    Local / Remote path to json file, where the dataframe is to be imported from.

Returns:



35
36
37
38
39
# File 'lib/daru/io/importers/json.rb', line 35

def read(path)
  @file_data = ::JSON.parse(open(path).read)
  @json      = @file_data
  self
end

Instance Method Details

#call(*columns, order: nil, index: nil, **named_columns) ⇒ Daru::DataFrame

Note:

For more information on using JSON-path selectors, have a look at the explanations here and here.

Imports a Daru::DataFrame from a JSON Importer instance

Examples:

Importing without jsonpath selectors

df = simple_read_instance.call

#=> #<Daru::DataFrame(202x10)>
#    designation discovery_      h_mag      i_deg    moid_au orbit_clas  period_yr ...
#  0 419880 (20 2011-01-07       19.7       9.65      0.035     Apollo       4.06  ...
#  1 419624 (20 2010-09-17       20.5      14.52      0.028     Apollo          1  ...
#  2 414772 (20 2010-07-28         19      23.11      0.333     Apollo       1.31  ...
# ...        ...        ...        ...        ...        ...        ...       ...  ...

Importing with jsonpath selectors

df = complex_read_instance.call(
  "$.._embedded..episodes..name",
  "$.._embedded..episodes..season",
  "$.._embedded..episodes..number",
  index: (10..70).to_a,
  RunTime: "$.._embedded..episodes..runtime"
)

#=> #<Daru::DataFrame(61x4)>
#         name           season     number    RunTime
#   10 Winter is           1          1         60
#   11 The Kingsr          1          2         60
#   12  Lord Snow          1          3         60
#  ...        ...        ...        ...        ...

Importing from from method

df = from_instance.call

#=> #<Daru::DataFrame(2x3)>
#       x   y   z
#   0   1   2   3
#   1   4   5   6

Parameters:

  • columns (Array)

    JSON-path slectors to select specific fields from the JSON input.

  • order (String or Array) (defaults to: nil)

    Either a JSON-path selector string, or an array containing the order of the Daru::DataFrame.

  • index (String or Array) (defaults to: nil)

    Either a JSON-path selector string, or an array containing the order of the Daru::DataFrame.

  • named_columns (Hash)

    JSON-path slectors to select specific fields from the JSON input.

Returns:



107
108
109
110
111
112
113
114
115
# File 'lib/daru/io/importers/json.rb', line 107

def call(*columns, order: nil, index: nil, **named_columns)
  init_opts(*columns, order: order, index: index, **named_columns)
  @data    = fetch_data
  @index   = at_jsonpath(@index)
  @order   = at_jsonpath(@order)
  @order ||= Array.new(@columns.count) { |x| x } + @named_columns.keys

  Daru::DataFrame.new(@data, order: @order, index: @index)
end

#from(instance) ⇒ Daru::IO::Importers::JSON

Loads from a Ruby structure of Hashes and / or Arrays

Examples:

Loading from Ruby Hash of Arrays

from_instance = Daru::IO::Importers::JSON.from({x: [1,4], y: [2,5], z: [3, 6]})

Parameters:

  • instance (Hash or Array)

    A simple / complexly nested JSON structure

Returns:



51
52
53
54
55
# File 'lib/daru/io/importers/json.rb', line 51

def from(instance)
  @file_data = instance
  @json      = @file_data.is_a?(String) ? ::JSON.parse(@file_data) : @file_data
  self
end

#read(path) ⇒ Daru::IO::Importers::JSON

Reads data from a json file / remote json response

Examples:

Reading from simply nested remote json response

url = 'https://data.nasa.gov/resource/2vr3-k9wn.json'
simple_read_instance = Daru::IO::Importers::JSON.read(url)

Reading from complexy nested remote json response

url = 'http://api.tvmaze.com/singlesearch/shows?q=game-of-thrones&embed=episodes'
complex_read_instance = Daru::IO::Importers::JSON.read(url)

Parameters:

  • path (String)

    Local / Remote path to json file, where the dataframe is to be imported from.

Returns:



35
36
37
38
39
# File 'lib/daru/io/importers/json.rb', line 35

def read(path)
  @file_data = ::JSON.parse(open(path).read)
  @json      = @file_data
  self
end