json_converter 0.0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/json_converter.rb +114 -0
  3. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4a090c2acc05c60d619aa49333e2df9e4317c287
4
+ data.tar.gz: 77216c648e6226a7ecff87a227f40ad5f4415043
5
+ SHA512:
6
+ metadata.gz: 55b00baccaa4c6a8d00b91e0f15d210cc08b60c28107bf7f8b95291d738523c94474b1d4ec39b02907bcfe8016539e8382249a2e8a4cfc26c92862daa7cdb377
7
+ data.tar.gz: 9224a76fd2d73704fdfee2a3410d4f91582fa5463ce1e84c4d872589d1c30faf7a82b7197fa0fa6515220868383c2a623795467a38c45ad885764ada568b4740
@@ -0,0 +1,114 @@
1
+ require 'csv'
2
+ require 'json'
3
+
4
+ class JsonConverter
5
+ # Generate and return a csv representation of the data
6
+ def generate_csv(json, headers=true, nil_substitute='')
7
+ csv = convert_to_csv json, nil_substitute
8
+ headers_written = false
9
+
10
+ generated_csv = CSV.generate do |output|
11
+ csv.each do |row|
12
+ if headers_written === false && headers === true
13
+ output << row.keys && headers_written = true
14
+ end
15
+
16
+ output << row.values
17
+ end
18
+ end
19
+
20
+ generated_csv
21
+ end
22
+
23
+ # Generate a csv representation of the data, then write to file
24
+ def write_to_csv(json, output_filename='out.csv', headers=true, nil_substitute='')
25
+ csv = convert_to_csv json, nil_substitute
26
+ headers_written = false
27
+
28
+ CSV.open(output_filename.to_s, 'w') do |output_file|
29
+ csv.each do |row|
30
+ if headers_written === false && headers === true
31
+ output_file << row.keys && headers_written = true
32
+ end
33
+
34
+ output_file << row.values
35
+ end
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ # Perform the actual conversion
42
+ def convert_to_csv(json, nil_substitute)
43
+ json = JSON.parse json if json.is_a? String
44
+
45
+ in_array = array_from json
46
+ out_array = []
47
+
48
+ # Replace all nil values with the value of nil_substitute; The presence
49
+ # of nil values in the data will usually result in uneven rows
50
+ in_array.map! { |x| nils_to_strings x, nil_substitute }
51
+
52
+ in_array.each do |row|
53
+ out_array[out_array.length] = flatten row
54
+ end
55
+
56
+ out_array
57
+ end
58
+
59
+ # Recursively convert all nil values of a hash to a specified string
60
+ def nils_to_strings(hash, replacement)
61
+ hash.each_with_object({}) do |(k,v), object|
62
+ case v
63
+ when Hash
64
+ object[k] = nils_to_strings v, replacement
65
+ when nil
66
+ object[k] = replacement.to_s
67
+ else
68
+ object[k] = v
69
+ end
70
+ end
71
+ end
72
+
73
+ # Recursively flatten a hash (or array of hashes)
74
+ def flatten(target, path='')
75
+ scalars = [String, Integer, Fixnum, FalseClass, TrueClass]
76
+ columns = {}
77
+
78
+ if [Hash, Array].include? target.class
79
+ target.each do |k, v|
80
+ new_columns = flatten(v, "#{path}#{k}/") if target.class == Hash
81
+ new_columns = flatten(k, "#{path}#{k}/") if target.class == Array
82
+ columns = columns.merge new_columns
83
+ end
84
+
85
+ return columns
86
+ elsif scalars.include? target.class
87
+ # Remove trailing slash from path
88
+ end_path = path[0, path.length - 1]
89
+ columns[end_path] = target
90
+ return columns
91
+ else
92
+ return {}
93
+ end
94
+ end
95
+
96
+ # Attempt to identify what elements of a hash are best represented as rows
97
+ def array_from(json_hash)
98
+ queue, next_item = [], json_hash
99
+ while !next_item.nil?
100
+
101
+ return next_item if next_item.is_a? Array
102
+
103
+ if next_item.is_a? Hash
104
+ next_item.each do |k, v|
105
+ queue.push next_item[k]
106
+ end
107
+ end
108
+
109
+ next_item = queue.shift
110
+ end
111
+
112
+ return [json_hash]
113
+ end
114
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_converter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Escue
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple class to generate CSV strings (or files) given a JSON object
14
+ or valid JSON string.
15
+ email: chris@escue.io
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/json_converter.rb
21
+ homepage: https://github.com/cescue/json-to-csv
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.4.5
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: A small gem for converting JSON data into a tabular format (CSV)
45
+ test_files: []