-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json-parse.sh
executable file
·145 lines (145 loc) · 2.96 KB
/
json-parse.sh
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
#!/usr/bin/env bash
json-parse() {
[ -z "$1" ] && exit 1
input=$(cat "$1")
output=""
i=0
line_break='
'
exit=0
var_name=()
level=0
in_value=false
in_key=false
in_array=false
in_object=false
after_key=false
array_index=0
keyname=""
value=""
last_char=""
while [ $exit == "0" ]; do
char=${input:i:1}
case $char in
"\"")
if [ $in_key == true ]; then
in_key=false
var_name+=("$keyname")
keyname=""
elif [ $in_value == true ]; then
if [ "$last_char" == "\\" ]; then
value+="\\\""
else
in_value=false
output+="$(
IFS=_
echo "${var_name[*]}"
)=\"$value\"\n"
value=""
fi
else
if [ $after_key == false ]; then
in_key=true
else
in_value=true
fi
fi
;;
":")
if [ $in_key == false ] && [ $in_value == false ]; then
if [ $after_key == false ]; then
after_key=true
fi
elif [ $in_value == true ]; then
value+="$char"
fi
;;
"," | "}")
if [ $in_key == false ] && [ $in_value == false ]; then
if [ "$last_char" != "\"" ] && [ $after_key == true ]; then
output+="$(
IFS=_
echo "${var_name[*]}"
)=$value\n"
value=""
fi
unset "var_name[-1]"
if [ $in_array == true ] && [ $in_object == false ]; then
after_key=true
array_index=$((array_index + 1))
var_name+=("$array_index")
else
after_key=false
fi
if [ "$char" == "}" ] && [ $in_array == true ]; then
level=$((level - 1))
if [ $level == 0 ]; then
in_object=false
after_key=true
fi
fi
elif [ $in_value == true ]; then
value+="$char"
fi
;;
"{")
if [ $in_key == false ] && [ $in_value == false ]; then
if [ $after_key == true ]; then
after_key=false
in_object=true
if [ $in_array == true ]; then
level=$((level + 1))
fi
fi
elif [ $in_value == true ]; then
value+="$char"
fi
;;
"[")
if [ $in_key == false ] && [ $in_value == false ]; then
if [ $after_key == true ]; then
in_array=true
var_name+=("$array_index")
fi
elif [ $in_value == true ]; then
value+="$char"
fi
;;
"]")
if [ $in_key == false ] && [ $in_value == false ]; then
if [ $after_key == true ]; then
in_array=false
after_key=false
unset "var_name[-1]"
fi
elif [ $in_value == true ]; then
value+="$char"
fi
;;
*)
if [ $in_key == true ]; then
keyname+=$char
elif [ $in_value == true ]; then
value+=$char
elif [ $after_key == true ] && [[ $char =~ [0-9aeflnrstu] ]]; then
value+=$char
fi
;;
esac
if [ "$(echo -en "$char" | sed -E "s|\\S|match|g")" == "match" ]; then
last_char=$char
fi
i=$(("$i" + 1))
[ ${#input} == $i ] && exit=1
done
filtered_output=""
IFS=$'\n'
for line in ${output//\\n/$line_break}; do
if [ "${line: -1}" != "=" ] && [ "${line: -4}" != "null" ]; then
filtered_output+="${line//\\\\/\\}$line_break"
fi
done
echo -n "$filtered_output" > "$1"
source "$1"
rm "$1"
}