Class: Daru::IO::Exporters::JSON

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

Overview

JSON Exporter Class, that extends to_json, to_json_string and write_json methods to Daru::DataFrame instance variables

Constant Summary collapse

ORIENT_TYPES =
%i[index records split values].freeze

Instance Method Summary collapse

Methods inherited from Base

#optional_gem

Constructor Details

#initialize(dataframe, orient: :records, pretty: false, **jsonpaths, &block) ⇒ JSON

Initializes a JSON Exporter instance.

Examples:

Initializing a JSON Exporter instance

df = Daru::DataFrame.new(
  [
    {name: 'Jon Snow', age: 18, sex: 'Male'},
    {name: 'Rhaegar Targaryen', age: 54, sex: 'Male'},
    {name: 'Lyanna Stark', age: 36, sex: 'Female'}
  ],
  order: %i[name age sex],
  index: %i[child dad mom]
)

#=> #<Daru::DataFrame(3x3)>
#            name          age       sex
# child   Jon Snow         18       Male
# dad   Rhaegar Ta         54       Male
# mom   Lyanna Sta         36     Female

json_exporter = Daru::IO::Exporters::JSON

index_instance = json_exporter.new(df, orient: :index, pretty: true)
records_instance = json_exporter.new(df,orient: :records, pretty: true)
values_instance = json_exporter.new(df, orient: :values, pretty: true)
split_instance = json_exporter.new(df, orient: :split, pretty: true)
static_jsonpath_instance = json_exporter.new(
    df, pretty: true, name: '$.specific.name', age: '$.common.age', sex: '$.common.gender'
)
dynamic_jsonpath_instance = json_exporter.new(
    df, pretty: true, age: '$.{name}.age', sex: '$.{name}.gender'
)
block_instance = json_exporter.new(df, orient: :index, pretty: true) do |json|
  json.map { |j| [j.keys.first, j.values.first] }.to_h
end

Parameters:

  • dataframe (Daru::DataFrame)

    A dataframe to export

  • orient (Symbol) (defaults to: :records)

    Setting to export the data in a specific structure. Defaults to :records.

    • :values : Returns a 2D array containing the data in the DataFrame.
    • :split : Returns a Hash, containing keys :vectors, :index and :data.
    • :records : Returns an Array of Hashes with given JsonPath content.
    • :index : Returns a Hash of Hashes with index values as keys, and given JsonPath content as values.

    After choosing an :orient option, the JSON content can be manipulated before writing into the JSON file, by providing a block.

  • pretty (Boolean) (defaults to: false)

    When set to true, the data is pretty-printed to the JSON file.

  • jsonpaths (Hash)

    JsonPaths to export given vectors into a compexly nested JSON structure.



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/daru/io/exporters/json.rb', line 67

def initialize(dataframe, orient: :records, pretty: false, **jsonpaths, &block)
  require 'json'
  optional_gem 'jsonpath'

  super(dataframe)
  @block         = block
  @orient        = orient
  @pretty        = pretty
  @jsonpath_hash = jsonpaths.empty? ? nil : jsonpaths

  validate_params
end

Instance Method Details

#toArray or Hash

Exports a JSON Exporter instance to a Ruby structure comprising of Arrays & Hashes.

Examples:

With default orient: :records

records_instance.to

#=>
# [
#   {
#     "sex": "Male",
#     "age": 18,
#     "name": "Jon Snow"
#   },
#   {
#     "sex": "Male",
#     "age": 54,
#     "name": "Rhaegar Targaryen"
#   },
#   {
#     "sex": "Female",
#     "age": 36,
#     "name": "Lyanna Stark"
#   }
# ]

With orient: :index

index_instance.to

#=>
# [
#   {
#     "child": {
#       "sex": "Male",
#       "age": 18,
#       "name": "Jon Snow"
#     }
#   },
#   {
#     "dad": {
#       "sex": "Male",
#       "age": 54,
#       "name": "Rhaegar Targaryen"
#     }
#   },
#   {
#     "mom": {
#       "sex": "Female",
#       "age": 36,
#       "name": "Lyanna Stark"
#     }
#   }
# ]

With orient: :values

values_instance.to

#=>
# [
#   [
#     "Jon Snow",
#     "Rhaegar Targaryen",
#     "Lyanna Stark"
#   ],
#   [
#     18,
#     54,
#     36
#   ],
#   [
#     "Male",
#     "Male",
#     "Female"
#   ]
# ]

With orient: :split

split_instance.to

#=>
# {
#   "vectors": [
#     "name",
#     "age",
#     "sex"
#   ],
#   "index": [
#     "child",
#     "dad",
#     "mom"
#   ],
#   "data": [
#     [
#       "Jon Snow",
#       "Rhaegar Targaryen",
#       "Lyanna Stark"
#     ],
#     [
#       18,
#       54,
#       36
#     ],
#     [
#       "Male",
#       "Male",
#       "Female"
#     ]
#   ]
# }

With static nested JsonPaths

static_jsonpath_instance.to

#=>
# [
#   {
#     "common": {
#       "gender": "Male",
#       "age": 18
#     },
#     "specific": {
#       "name": "Jon Snow"
#     }
#   },
#   {
#     "common": {
#       "gender": "Male",
#       "age": 54
#     },
#     "specific": {
#       "name": "Rhaegar Targaryen"
#     }
#   },
#   {
#     "common": {
#       "gender": "Female",
#       "age": 36
#     },
#    "specific": {
#       "name": "Lyanna Stark"
#     }
#   }
# ]

With dynamic JsonPaths

dynamic_jsonpath_instance.to

#=>
# [
#   {
#     "Jon Snow": {
#       "gender": "Male",
#       "age": 18
#     }
#   },
#   {
#     "Rhaegar Targaryen": {
#       "gender": "Male",
#       "age": 54
#     }
#   },
#   {
#     "Lyanna Stark": {
#     "gender": "Female",
#     "age": 36
#     }
#   }
# ]

With orient: :index and block

block_instance.to

#=>
# {
#   "child": {
#     "sex": "Male",
#     "age": 18,
#     "name": "Jon Snow"
#   },
#   "dad": {
#     "sex": "Male",
#     "age": 54,
#     "name": "Rhaegar Targaryen"
#   },
#   "mom": {
#     "sex": "Female",
#     "age": 36,
#     "name": "Lyanna Stark"
#   }
# }

Returns:

  • (Array or Hash)


464
465
466
467
468
469
470
471
472
# File 'lib/daru/io/exporters/json.rb', line 464

def to
  @jsonpath_hash ||= @dataframe.vectors.to_a.map { |v| {v => "$.#{v}"} }.reduce(:merge)
  @vectors         = @jsonpath_hash.keys
  @jsonpaths       = process_jsonpath
  @json_content    = process_json_content
  @json_content    = @block.call(@json_content) if @block

  @json_content
end

#to_sString

Exports a JSON Exporter instance to a file-writable String.

Examples:

Getting a file-writable string with default orient: :records

records_instance.to_s

#=>
# [
#   {
#     "sex": "Male",
#     "age": 18,
#     "name": "Jon Snow"
#   },
#   {
#     "sex": "Male",
#     "age": 54,
#     "name": "Rhaegar Targaryen"
#   },
#   {
#     "sex": "Female",
#     "age": 36,
#     "name": "Lyanna Stark"
#   }
# ]

Getting a file-writable string with orient: :index

index_instance.to_s

#=>
# [
#   {
#     "child": {
#       "sex": "Male",
#       "age": 18,
#       "name": "Jon Snow"
#     }
#   },
#   {
#     "dad": {
#       "sex": "Male",
#       "age": 54,
#       "name": "Rhaegar Targaryen"
#     }
#   },
#   {
#     "mom": {
#       "sex": "Female",
#       "age": 36,
#       "name": "Lyanna Stark"
#     }
#   }
# ]

Getting a file-writable string with orient: :values

values_instance.to_s

#=>
# [
#   [
#     "Jon Snow",
#     "Rhaegar Targaryen",
#     "Lyanna Stark"
#   ],
#   [
#     18,
#     54,
#     36
#   ],
#   [
#     "Male",
#     "Male",
#     "Female"
#   ]
# ]

Getting a file-writable string with orient: :split

split_instance.to_s

#=>
# {
#   "vectors": [
#     "name",
#     "age",
#     "sex"
#   ],
#   "index": [
#     "child",
#     "dad",
#     "mom"
#   ],
#   "data": [
#     [
#       "Jon Snow",
#       "Rhaegar Targaryen",
#       "Lyanna Stark"
#     ],
#     [
#       18,
#       54,
#       36
#     ],
#     [
#       "Male",
#       "Male",
#       "Female"
#     ]
#   ]
# }

Getting a file-writable string with static nested JsonPaths

static_jsonpath_instance.to_s

#=>
# [
#   {
#     "common": {
#       "gender": "Male",
#       "age": 18
#     },
#     "specific": {
#       "name": "Jon Snow"
#     }
#   },
#   {
#     "common": {
#       "gender": "Male",
#       "age": 54
#     },
#     "specific": {
#       "name": "Rhaegar Targaryen"
#     }
#   },
#   {
#     "common": {
#       "gender": "Female",
#       "age": 36
#     },
#    "specific": {
#       "name": "Lyanna Stark"
#     }
#   }
# ]

Getting a file-writable string with dynamic JsonPaths

dynamic_jsonpath_instance.to_s

#=>
# [
#   {
#     "Jon Snow": {
#       "gender": "Male",
#       "age": 18
#     }
#   },
#   {
#     "Rhaegar Targaryen": {
#       "gender": "Male",
#       "age": 54
#     }
#   },
#   {
#     "Lyanna Stark": {
#     "gender": "Female",
#     "age": 36
#     }
#   }
# ]

Getting a file-writable string with orient: :index and block

block_instance.to_s

#=>
# {
#   "child": {
#     "sex": "Male",
#     "age": 18,
#     "name": "Jon Snow"
#   },
#   "dad": {
#     "sex": "Male",
#     "age": 54,
#     "name": "Rhaegar Targaryen"
#   },
#   "mom": {
#     "sex": "Female",
#     "age": 36,
#     "name": "Lyanna Stark"
#   }
# }

Returns:

  • (String)

    A file-writable string



270
271
272
# File 'lib/daru/io/exporters/json.rb', line 270

def to_s
  super
end

#write(path) ⇒ Object

Exports a JSON Exporter instance to a json file.

Examples:

Writing a JSON Exporter instance to a JSON file

index_instance.write('index.json')
split_instance.write('split.json')
values_instance.write('values.json')
records_instance.write('records.json')
static_jsonpath_instance.write('static.json')
dynamic_jsonpath_instance.write('dynamic.json')
block_instance.write('block.json')

Parameters:

  • path (String)

    Path of JSON file where the dataframe is to be saved



486
487
488
489
490
# File 'lib/daru/io/exporters/json.rb', line 486

def write(path)
  File.open(path, 'w') do |file|
    file.write(::JSON.send(@pretty ? :pretty_generate : :generate, to))
  end
end