-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathunheadify.rb
executable file
·105 lines (93 loc) · 2.12 KB
/
unheadify.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env ruby
# Converts json butchered by elasticsearch head plugin into real json
# Usage: ruby unheadify.rb < butchered.json > real.json
require 'rubygems'
require 'json'
class Unheadify
def initialize()
@stdin = STDIN
end
def run
normalize(parse())
end
def normalize(index)
index.delete("state")
settings = index["settings"]
index["settings"] = reduce(index["settings"])
index
end
def reduce(settings)
new_settings = {}
settings.each do |key, value|
put(new_settings, key, value)
end
new_settings
end
def put(hash, key, value)
pos = key.index(".")
if pos
head = key[0..pos-1]
tail = key[pos+1..-1]
if !hash.has_key?(head)
if is_numeric?(tail)
hash[head] = []
else
hash[head] = {}
end
end
put(hash[head], tail, value)
else
if is_numeric?(key)
hash[Integer(key)] = value
else
hash[key] = value
end
end
end
def parse
output = ""
commas = [false];
first = true;
@stdin.each do |line|
line.strip!
if line == '{' || line == '}' || line == ']'
if first
first = false
else
output << "\n"
end
output << line
else
if commas[-1]
output << ","
end
output << "\n"
(key, val) = line.split(": ")
if val
if val == "true" || val == "false" || is_numeric?(val) || line.end_with?("{") || line.end_with?("[")
output << "\"" << key << "\": " << val
else
output << "\"" << key << "\": \"" << val << "\""
end
else
output << "\"" << key << "\""
end
commas[-1] = true
end
if line.end_with?("{") || line.end_with?("[")
commas.push(false)
elsif line.end_with?("}") || line.end_with?("]")
commas.pop()
end
end
JSON.parse(output)
end
def is_numeric?(val)
Float(val)
true
rescue
false
end
end
# puts Unheadify.new().run
puts JSON.pretty_generate(Unheadify.new().run)