-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonParser.sh
executable file
·178 lines (156 loc) · 3.54 KB
/
jsonParser.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
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
#!/bin/bash
#
# This file is taken from https://github.com/lirik90/bashJsonParser
# Public JSON parser function
function parseJson() {
parseJsonImpl 1 0 "$@"
}
# Public JSON minifier function
function minifyJson() {
local -i escaped=0 quoted=0 i
local s="$1" c="" res=""
for ((i=0; i<${#s}; ++i)); do
c="${s:i:1}"
if [ $escaped -eq 1 ]; then
escaped=0
if [ "$c" = "n" ] || [ "$c" = "r" ] && [ $quoted -eq 0 ]; then
res="${res:0:-1}"
else
res+="$c"
fi
continue
fi
if [ "$c" = "\\" ]; then
escaped=1
elif [ "$c" = "\"" ]; then
[ $quoted -eq 1 ] && quoted=0 || quoted=1
fi
if [ "$c" = " " ] || [[ "$c" == $'\n' ]] || [[ "$c" == $'\r' ]]; then
[ $quoted -eq 0 ] && continue
fi
res+="$c"
done
echo "$res"
}
# 'Open'-'Close' braces calculator
function calcBraces() {
local -i sum=$1 escaped=0 quoted=0 i
local s="$2" c=""
for ((i=0; i<${#s}; ++i)); do
processJson
[ $? -ne 0 ] && break
done
echo $sum
}
# Calc length one object from 'open' to 'close' braces
function jsonObjectLength() {
local -i sum=1 escaped=0 quoted=0 i
local s="$1" c=""
# Check first char is open braces
[ "${s:0:1}" = "{" ] && for ((i=1; i<${#s}; ++i)); do
if [ $sum -eq 0 ]; then
echo $i
return
fi
processJson
[ $? -ne 0 ] && break
done
echo 0
}
# Helper function for filter data in JSON stream
function processJson() {
if [ $escaped -eq 1 ]; then
escaped=0
return 0
fi
c="${s:i:1}"
if [ "$c" = "\\" ]; then
escaped=1
elif [ "$c" = "\"" ]; then
[ $quoted -eq 1 ] && quoted=0 || quoted=1
fi
[ $quoted -eq 1 ] && return 0
if [ "$c" = "{" ]; then
sum=$((sum+1))
elif [ "$c" = "}" ]; then
sum=$((sum-1))
[ $sum -lt 0 ] && return 1
fi
return 0
}
function parseJsonImpl() {
local -i depth=$1 br=$2 pos=0 i
local JSON="$3" m=""
shift; shift; shift
local obj=("$@")
while true; do
if [[ ${obj[0]} =~ ^[0-9]+$ ]]; then
# Check is array under position
[ "${JSON:0:1}" = "[" ] || return 1
JSON="${JSON:1}"
for ((i=0; i<${obj[0]}; ++i)); do
m=$(printCurrentJsonValue "${JSON}" --keepQuotes)
[ $? -ne 0 ] && return 1
JSON="${JSON:${#m}}"
# Check is next object available
[ "${JSON:0:1}" = "," ] || return 1
JSON="${JSON:1}"
done
# Check is next object under position
if [ "${JSON:0:1}" = "{" ]; then
JSON="${JSON:1}"
br=$((br+1))
depth=$((depth-1))
fi
break
fi
m="${JSON%%"\"${obj[0]}\":"*}"
[[ "$m" = "$JSON" ]] && return 1
pos=$((${#m}+${#obj[0]}+3))
JSON="${JSON:$pos}"
[ -z "$m" ] && break
br=$(calcBraces $br "$m")
[ $br -eq $depth ] && break
done
if [ ${#obj[@]} -gt 1 ]; then
unset 'obj[0]'
parseJsonImpl $((depth+1)) $br "${JSON}" "${obj[@]}"
else
printCurrentJsonValue "$JSON"
fi
return $?
}
# Print JSON value from beginning of arg
function printCurrentJsonValue() {
local JSON="$1"
if [[ $JSON =~ ^\"(([^\"]|\\\")*)\" ]]; then
local quote=""
[ "$2" = "--keepQuotes" ] && quote="\""
echo "${quote}${BASH_REMATCH[1]}${quote}"
elif [[ $JSON =~ ^(-?[0-9]+) ]]; then
echo "${BASH_REMATCH[1]}"
elif [[ $JSON =~ ^(true|false|null) ]]; then
echo "${BASH_REMATCH[1]}"
elif [[ $JSON =~ ^\[ ]]; then
local res="[" item
local -i pos=1
while true; do
res+=$(printCurrentJsonValue "${JSON:$pos}" --keepQuotes)
[ $? -ne 0 ] && break
pos=${#res}
if [ "${JSON:$pos:1}" = "," ]; then
res+=","
pos=$((pos+1))
fi
done
echo "${res}]"
elif [[ $JSON =~ ^\{ ]]; then
local -i len
len=$(jsonObjectLength "$JSON")
[ $len -eq 0 ] && return 1
echo "${JSON:0:$len}"
else
return 1
fi
return 0
}