csv_json_converter 0.1.1 → 0.1.3
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.
- checksums.yaml +4 -4
- data/README.md +7 -11
- data/lib/csv_json_converter/version.rb +1 -1
- data/lib/csv_json_converter.rb +37 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4a861d0f37952316e996ed45a1838727f8639cbeac64b393b6f0f10b6444822
|
4
|
+
data.tar.gz: 712b4ac2af69d0e11441338e1ab190e2d93f0169ba8d21937c1dce41d1618c85
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 668f30cfdfe949fdc2c4f6998428d9da52020ea5175682b7ef958c9f80c0f3cb61d1864b4a9a4d9df2d7b642e306b4bdb433632cad8815747d32545a0d76a748
|
7
|
+
data.tar.gz: 9ea7c0e6f7203587a295201f2871e918b8f40395eba99a2958be53267ec5055dc271d97892adf17fcd97912d863c67fa1768e77f44665c2ae1eaec873173c1f3
|
data/README.md
CHANGED
@@ -13,8 +13,8 @@
|
|
13
13
|
- [:busts_in_silhouette: Authors ](#-authors-)
|
14
14
|
- [:handshake: Contributing ](#-contributing-)
|
15
15
|
- [:star:️ Show your support ](#️-show-your-support-)
|
16
|
-
# :book:
|
17
|
-
This gem converts
|
16
|
+
# :book: csv_json_convert <a name="about-project"></a>
|
17
|
+
This gem converts a csv string to json object.
|
18
18
|
## Installation <a name="tech-stack"></a>
|
19
19
|
Add the following code to you Gemfile
|
20
20
|
```
|
@@ -32,25 +32,21 @@ If you want to convert `csv string`, to json you can use `to_json` method from `
|
|
32
32
|
|
33
33
|
<i>For example </i>
|
34
34
|
```ruby
|
35
|
-
|
35
|
+
|
36
36
|
require 'csv_json_converter'
|
37
37
|
|
38
38
|
data_csv='file;text;number;hex
|
39
39
|
test18.csv;CMkABfAGXvmSFV;9892576;jz40cbafbec8d6f92e93d22ea6ef5b'
|
40
40
|
|
41
|
-
json_data = CsvJsonCoverter
|
41
|
+
json_data = CsvJsonCoverter.to_json(data_csv)
|
42
42
|
|
43
43
|
print json_data
|
44
|
-
|
45
|
-
# "text": "CMkABfAGXvmSFV",
|
46
|
-
# "number": 9892576,
|
47
|
-
# "hex": "jz40cbafbec8d6f92e93d22ea6ef5b"
|
48
|
-
#}
|
49
|
-
#```
|
44
|
+
|
50
45
|
```
|
51
46
|
|
52
47
|
**The output looks like below**
|
53
|
-
```
|
48
|
+
```
|
49
|
+
{ "file": "test18.csv",
|
54
50
|
"text": "CMkABfAGXvmSFV",
|
55
51
|
"number": 9892576,
|
56
52
|
"hex": "jz40cbafbec8d6f92e93d22ea6ef5b"
|
data/lib/csv_json_converter.rb
CHANGED
@@ -6,16 +6,44 @@ require_relative "csv_json_converter/version"
|
|
6
6
|
module CsvJsonConverter
|
7
7
|
class Error < StandardError; end
|
8
8
|
# Your code goes here...
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
9
|
+
def self.to_json(input, separator=',')
|
10
|
+
for_csv = CSV.parse(input,:col_sep => separator,headers: true, header_converters: :symbol)
|
11
|
+
# treat first row as headers if the caller didn't provide them
|
12
|
+
arr_to_json = for_csv.map(&:to_h).to_json
|
13
|
+
|
14
|
+
arr_to_json
|
15
|
+
|
16
16
|
|
17
|
-
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.to_csv(input)
|
20
|
+
|
21
|
+
for_json=JSON.parse(input)
|
22
|
+
column_names = for_json.first.keys
|
23
|
+
csv_string=CSV.generate do |csv|
|
24
|
+
csv << column_names
|
25
|
+
for_json.each do |x|
|
26
|
+
csv << x.values
|
27
|
+
end
|
18
28
|
end
|
19
|
-
|
29
|
+
|
30
|
+
csv_string
|
31
|
+
|
20
32
|
end
|
21
33
|
end
|
34
|
+
|
35
|
+
|
36
|
+
p arr_json=CsvJsonConverter.to_json('file,text,number,hex
|
37
|
+
test18.csv,CMkABfAGXvmSFV,9892576,jz40cbafbec8d6f92e93d22ea6ef5b
|
38
|
+
test18.csv,NCVTagVs,815097234,jz0ee6f01d178831228b957376f15c
|
39
|
+
test18.csv,RCZgwkLosc,3,jz22ff45479c30a890925d5832c319
|
40
|
+
test18.csv,HZgYgaBNZFXyVdzuRuddpg,31,jz737707427673939e4c198598cefc
|
41
|
+
test18.csv,tfp,6,jzedd110611f58b53215dd0b27f7ba
|
42
|
+
')
|
43
|
+
|
44
|
+
arr_csv=CsvJsonConverter.to_csv(
|
45
|
+
arr_json
|
46
|
+
)
|
47
|
+
|
48
|
+
p arr_csv
|
49
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv_json_converter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- alexoid1
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-08-
|
11
|
+
date: 2023-08-25 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: This gem transform a csv string in a json string.
|
14
14
|
email:
|