json_normalizer 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/lib/json_normalizer.rb +35 -12
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b1855581218bf47b302951a5547c085e4cbf05a
|
4
|
+
data.tar.gz: 13657ff88bf9a9af352f20fc4620d818de0d9c0c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b618106c9863dbf04235bf202ac6b74e39af1fc095922794f37fe761f4239ae62b0abc61dfbb7b00065c670dc355c7a43c357488576f004cfa97ba88237f066
|
7
|
+
data.tar.gz: b30c1daa700c300e1b003a378734a906298e1f5ef6394b6720b76bc54c4e0593d873a5e322072d9f3346e563f294d1c9cb2c74c961c4228db4d55ec16cb6294b
|
data/lib/json_normalizer.rb
CHANGED
@@ -19,29 +19,52 @@ class Json_Normalizer
|
|
19
19
|
@map.keys.each{ |k| return k if @map[k].include?(key) }
|
20
20
|
end
|
21
21
|
|
22
|
+
# def swap_key(json, key)
|
23
|
+
# if self.key_contained?(key)
|
24
|
+
# json[self.fetch_key(key).first] = json[key]
|
25
|
+
# json.delete(key)
|
26
|
+
# else
|
27
|
+
# # if we want to move out non-normalized keys into a different 'misc' key or something
|
28
|
+
# end
|
29
|
+
# end
|
30
|
+
|
22
31
|
def swap_key(json, key)
|
23
32
|
if self.key_contained?(key)
|
24
|
-
json[self.fetch_key(key)
|
33
|
+
json[self.fetch_key(key)] = json[key]
|
25
34
|
json.delete(key)
|
26
|
-
else
|
27
|
-
# if we want to move out non-normalized keys into a different 'misc' key or something
|
28
35
|
end
|
29
36
|
end
|
30
37
|
|
31
38
|
def translate(json)
|
32
|
-
json = JSON.parse(json) if !json.is_a?(Hash)
|
33
|
-
json.
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
39
|
+
# json = JSON.parse(json) if !json.is_a?(Hash)
|
40
|
+
json = JSON.parse(json) if ![Hash, Array].include?(json.class)
|
41
|
+
if json.is_a?(Array)
|
42
|
+
json.each do |j|
|
43
|
+
j.keys.each do |key|
|
44
|
+
if j[key].respond_to?(:each)
|
45
|
+
puts "Key: #{key} recursing..."
|
46
|
+
self.translate(j[key])
|
47
|
+
self.swap_key(j, key)
|
48
|
+
else
|
49
|
+
self.swap_key(j, key)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
else
|
54
|
+
json.keys.each do |key|
|
55
|
+
if json[key].respond_to?(:each)
|
56
|
+
puts "Key: #{key} recursing..."
|
57
|
+
self.translate(json[key])
|
58
|
+
self.swap_key(json, key)
|
59
|
+
else
|
60
|
+
self.swap_key(json, key)
|
61
|
+
end
|
40
62
|
end
|
41
63
|
end
|
42
64
|
|
43
65
|
# json.map{ |k, v| [k.to_s, v] }.to_h
|
44
|
-
JSON.parse json.to_json
|
66
|
+
# JSON.parse json.to_json
|
67
|
+
json
|
45
68
|
end
|
46
69
|
|
47
70
|
end
|