json_csv_converter 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +2 -0
- data/README.md +47 -0
- data/Rakefile +19 -0
- data/bin/json_csv_converter +4 -0
- data/json_csv_converter.gemspec +22 -0
- data/lib/json_csv_converter/converter.rb +37 -0
- data/lib/json_csv_converter/version.rb +3 -0
- data/lib/json_csv_converter.rb +15 -0
- data/spec/spec_helper.rb +6 -0
- metadata +90 -0
data/.gitignore
ADDED
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# JSON CSV Converter
|
2
|
+
This gem is built to help at the migration between existing JSON files into the mappings of existing CSV tables and vice versa.
|
3
|
+
To map the keys you provide a YAML file.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'json_csv_converter'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install json_csv_converter
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
To convert the data of an existing JSON file into an existing CSV table just run `JSONCSVConverter::json_to_csv` with the JSON path, the CSV path and the YAML mapping file path as arguments. This method returns a `CSV::Table`.
|
22
|
+
|
23
|
+
The gem also provides an executable named `json_csv_converter`. It currently only supports conversion from JSON to CSV.
|
24
|
+
If you run
|
25
|
+
|
26
|
+
$ json_csv_converter <i>path_to_json</i> <i>path_to_csv</i> <i>path_to_yaml_mapping</i>
|
27
|
+
|
28
|
+
you get the completely converted CSV table as String for further processing.
|
29
|
+
|
30
|
+
### Example:
|
31
|
+
|
32
|
+
~~~ruby
|
33
|
+
# create an instance to work with
|
34
|
+
converter = JSONCSVConverter::Converter.new(json_path,csv_path,mapping_path)
|
35
|
+
|
36
|
+
# map each JSON Object to a CSV row
|
37
|
+
converter.json_to_csv
|
38
|
+
|
39
|
+
# get the CSV table
|
40
|
+
csv_table = converter.csv_table
|
41
|
+
|
42
|
+
# get the JSON instance
|
43
|
+
json = converter.json
|
44
|
+
|
45
|
+
# Or simply run the toplevel method
|
46
|
+
csv_table = JSONCSVConverter.json_to_csv(json_path,csv_path,mapping_path)
|
47
|
+
~~~
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
|
4
|
+
Bundler::GemHelper.install_tasks
|
5
|
+
RSpec::Core::RakeTask.new('spec')
|
6
|
+
|
7
|
+
task :default => :spec
|
8
|
+
|
9
|
+
desc "Create spec for class or module"
|
10
|
+
task :create_spec, [:class_name] do |t,args|
|
11
|
+
class_name = args.class_name
|
12
|
+
File.open("spec/#{class_name}_spec","w") do |file|
|
13
|
+
file.puts "require 'spec_helper'"
|
14
|
+
file.puts
|
15
|
+
file.puts %(describe #{class_name} do)
|
16
|
+
file.puts " # Your test here"
|
17
|
+
file.puts "end"
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "json_csv_converter/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "json_csv_converter"
|
7
|
+
s.version = JSONCSVConverter::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Michael Wagner"]
|
10
|
+
s.email = ["mitch.wagna@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/mitch000001/json_csv_converter"
|
12
|
+
s.summary = %q{A converter between JSON and CSV files}
|
13
|
+
s.description = %q{This gem provides a converter between an existing JSON file and an existing CSV file. The Mapping is provided via YAML.}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency 'json'
|
21
|
+
s.add_development_dependency 'rspec'
|
22
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module JSONCSVConverter
|
2
|
+
|
3
|
+
class Converter
|
4
|
+
|
5
|
+
attr_reader :csv_table, :json, :mapping
|
6
|
+
|
7
|
+
def initialize(json,csv,mapping,options={})
|
8
|
+
@json_path = json
|
9
|
+
@json_name = options[:json_name] || File.basename(json,'.json')
|
10
|
+
@json = JSON.load(File.open(json))
|
11
|
+
@csv_path = csv
|
12
|
+
@csv_table = CSV.table(csv,options)
|
13
|
+
@mapping = Psych.load_file(mapping)
|
14
|
+
end
|
15
|
+
|
16
|
+
def json_to_csv
|
17
|
+
headers = @csv_table.headers
|
18
|
+
@json[@json_name].each do |element|
|
19
|
+
row = CSV::Row.new([],[])
|
20
|
+
headers.each do |column|
|
21
|
+
column_hash = {}
|
22
|
+
mapping_key = @mapping["to_csv"][column.to_s]
|
23
|
+
if mapping_key.kind_of? Array
|
24
|
+
column_hash[column] = mapping_key.inject(element) {|whole,map_key|whole = whole[map_key]}
|
25
|
+
else
|
26
|
+
column_hash[column] = element[mapping_key]
|
27
|
+
end
|
28
|
+
row << column_hash
|
29
|
+
end
|
30
|
+
@csv_table << row
|
31
|
+
end
|
32
|
+
self
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'csv'
|
3
|
+
require 'json'
|
4
|
+
require 'psych'
|
5
|
+
|
6
|
+
require 'json_csv_converter/version'
|
7
|
+
require 'json_csv_converter/converter'
|
8
|
+
|
9
|
+
module JSONCSVConverter
|
10
|
+
|
11
|
+
def json_to_csv json,csv,mapping,option={}
|
12
|
+
Converter.new(json,csv,mapping,options).json_to_csv.csv_table
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json_csv_converter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Michael Wagner
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: json
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: This gem provides a converter between an existing JSON file and an existing
|
47
|
+
CSV file. The Mapping is provided via YAML.
|
48
|
+
email:
|
49
|
+
- mitch.wagna@gmail.com
|
50
|
+
executables:
|
51
|
+
- json_csv_converter
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- bin/json_csv_converter
|
59
|
+
- json_csv_converter.gemspec
|
60
|
+
- lib/json_csv_converter.rb
|
61
|
+
- lib/json_csv_converter/converter.rb
|
62
|
+
- lib/json_csv_converter/version.rb
|
63
|
+
- spec/spec_helper.rb
|
64
|
+
homepage: https://github.com/mitch000001/json_csv_converter
|
65
|
+
licenses: []
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.8.24
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: A converter between JSON and CSV files
|
88
|
+
test_files:
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
has_rdoc:
|