-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe3.run
More file actions
258 lines (217 loc) · 6.36 KB
/
Copy pathe3.run
File metadata and controls
258 lines (217 loc) · 6.36 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env bash
PATH=.
usage() {
echo "Usage: $0 [--keys | --data | KEY] <json_file>"
echo " Only one of the following will be printed:"
echo " --keys Print all keys from the JSON file"
echo " --data Print all data from the JSON file"
echo " KEY A hierarchical key (dot separated) to extract its value"
echo " <json_file> The JSON file to process"
exit 1
}
if [ "$#" -lt 1 ]; then
usage
fi
keys_flag=0
data_flag=0
specific_key=""
json_file=""
while [ "$#" -gt 0 ]; do
case "$1" in
--keys)
keys_flag=1 ;;
--data)
data_flag=1 ;;
*)
if [ "$#" -eq 1 ]; then
json_file="$1"
else
specific_key="$1"
fi
;;
esac
shift
done
if [ -z "$json_file" ]; then
echo "Error: JSON file not specified"
usage
fi
if [ ! -r "$json_file" ]; then
echo "Error: File '$json_file' does not exist or is not readable"
usage
fi
json=$(<"$json_file")
print_all_keys() {
local pos=0
local len=${#json}
while [ $pos -lt $len ]; do
if [ "${json:$pos:1}" = "\"" ]; then
pos=$((pos+1))
local start=$pos
while [ $pos -lt $len ] && [ "${json:$pos:1}" != "\"" ]; do
pos=$((pos+1))
done
local candidate="${json:$start:$((pos-start))}"
local rest="${json:$((pos+1))}"
while [ -n "$rest" ] && [[ "${rest:0:1}" =~ [[:space:]] ]]; do
rest="${rest:1}"
done
if [ "${rest:0:1}" = ":" ]; then
echo "$candidate"
fi
else
pos=$((pos+1))
fi
done
}
print_all_data() {
local pos=0
local len=${#json}
while [ $pos -lt $len ]; do
if [ "${json:$pos:1}" = ":" ]; then
pos=$((pos+1))
while [ $pos -lt $len ] && [[ "${json:$pos:1}" =~ [[:space:]] ]]; do
pos=$((pos+1))
done
local first_char="${json:$pos:1}"
if [ "$first_char" = "\"" ]; then
pos=$((pos+1))
local start=$pos
while [ $pos -lt $len ] && [ "${json:$pos:1}" != "\"" ]; do
pos=$((pos+1))
done
echo "${json:$start:$((pos-start))}"
elif [ "$first_char" = "{" ] || [ "$first_char" = "[" ]; then
local start_char="$first_char"
local end_char
if [ "$start_char" = "{" ]; then
end_char="}"
else
end_char="]"
fi
local count=0
local start=$pos
while [ $pos -lt $len ]; do
local ch="${json:$pos:1}"
if [ "$ch" = "$start_char" ]; then
count=$((count+1))
elif [ "$ch" = "$end_char" ]; then
count=$((count-1))
if [ $count -eq 0 ]; then
pos=$((pos+1))
break
fi
fi
pos=$((pos+1))
done
echo "${json:$start:$((pos-start))}"
else
local start=$pos
while [ $pos -lt $len ]; do
local ch="${json:$pos:1}"
if [ "$ch" = "," ] || [ "$ch" = "}" ] || [ "$ch" = "]" ] || [[ "$ch" =~ [[:space:]] ]]; then
break
fi
pos=$((pos+1))
done
local result="${json:$start:$((pos-start))}"
shopt -s extglob
result="${result%%+([[:space:]])}"
echo "$result"
fi
else
pos=$((pos+1))
fi
done
}
extract_value() {
local json_input="$1"
local key="$2"
local search="\"$key\""
local temp="${json_input#*$search}"
if [ "$temp" = "$json_input" ]; then
echo "KEY_NOT_FOUND"
return 1
fi
temp="${temp#*:}"
temp="${temp#"${temp%%[![:space:]]*}"}"
local first_char="${temp:0:1}"
if [ "$first_char" = "\"" ]; then
temp="${temp:1}"
local value="${temp%%\"*}"
echo "$value"
return 0
elif [ "$first_char" = "{" ] || [ "$first_char" = "[" ]; then
local start_char="$first_char"
local end_char
if [ "$start_char" = "{" ]; then
end_char="}"
else
end_char="]"
fi
local count=0
local pos=0
local len=${#temp}
local extracted=""
while [ $pos -lt $len ]; do
local ch="${temp:$pos:1}"
extracted="${extracted}${ch}"
if [ "$ch" = "$start_char" ]; then
count=$((count+1))
elif [ "$ch" = "$end_char" ]; then
count=$((count-1))
if [ $count -eq 0 ]; then
pos=$((pos+1))
break
fi
fi
pos=$((pos+1))
done
echo "$extracted"
return 0
else
local value=""
local pos=0
local len=${#temp}
while [ $pos -lt $len ]; do
local ch="${temp:$pos:1}"
if [ "$ch" = "," ] || [ "$ch" = "}" ] || [ "$ch" = "]" ] || [[ "$ch" =~ [[:space:]] ]]; then
break
fi
value="${value}${ch}"
pos=$((pos+1))
done
echo "$value"
return 0
fi
}
extract_hierarchical_value() {
local current_json="$1"
local key_path="$2"
local key="${key_path%%.*}"
local rest=""
if [ "$key_path" != "$key" ]; then
rest="${key_path#*.}"
fi
local value
value=$(extract_value "$current_json" "$key")
if [ "$value" = "KEY_NOT_FOUND" ]; then
echo "Key not found: $key_path"
return 1
fi
if [ -n "$rest" ]; then
echo "$(extract_hierarchical_value "$value" "$rest")"
else
echo "$value"
fi
}
if [ $keys_flag -eq 1 ]; then
print_all_keys
elif [ $data_flag -eq 1 ]; then
print_all_data
elif [ -n "$specific_key" ]; then
extract_hierarchical_value "$json" "$specific_key"
else
echo "No valid operation specified."
usage
fi