15 echo "Usage: JSON.sh [-b] [-l] [-p] [-s] [-h]"
17 echo "-p - Prune empty. Exclude fields with empty values."
18 echo "-l - Leaf only. Only show leaf nodes, which stops data duplication."
19 echo "-b - Brief. Combines 'Leaf only' and 'Prune empty' options."
20 echo "-s - Remove escaping of the solidus symbol (stright slash)."
21 echo "-h - This help text."
28 while [ "$ARGN" -ne
0 ]
42 -s
) NORMALIZE_SOLIDUS
=1
44 ?
*) echo "ERROR: Unknown option."
55 local pattern_string
=$1
58 start=match($0, pattern);
59 token=substr($0, start, RLENGTH);
61 $0=substr($0, start+RLENGTH);
63 }' pattern
="$pattern_string"
71 if echo "test string" |
egrep -ao
--color
=never
"test" &>/dev
/null
73 GREP
='egrep -ao --color=never'
78 if echo "test string" |
egrep -o
"test" &>/dev
/null
80 ESCAPE
='(\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'
81 CHAR
='[^[:cntrl:]"\\]'
84 ESCAPE
='(\\\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'
85 CHAR
='[^[:cntrl:]"\\\\]'
88 local STRING
="\"$CHAR*($ESCAPE$CHAR*)*\""
89 local NUMBER
='-?(0|[1-9][0-9]*)([.][0-9]*)?([eE][+-]?[0-9]*)?'
90 local KEYWORD
='null|false|true'
91 local SPACE
='[[:space:]]+'
93 $GREP "$STRING|$NUMBER|$KEYWORD|$SPACE|." |
egrep -v
"^$SPACE$"
105 parse_value
"$1" "$index"
112 *) throw
"EXPECTED , or ] GOT ${token:-EOF}" ;;
118 [ "$BRIEF" -eq
0 ] && value
=$
(printf '[%s]' "$ary") || value
=
132 '"'*'"') key
="$token" ;;
133 *) throw
"EXPECTED string GOT ${token:-EOF}" ;;
138 *) throw
"EXPECTED : GOT ${token:-EOF}" ;;
141 parse_value
"$1" "$key"
142 obj
="$obj$key:$value"
147 *) throw
"EXPECTED , or } GOT ${token:-EOF}" ;;
153 [ "$BRIEF" -eq
0 ] && value
=$
(printf '{%s}' "$obj") || value
=
158 local jpath
="${1:+$1.}$2" isleaf
=0 isempty
=0 print
=0
160 '{') parse_object
"$jpath" ;;
161 '[') parse_array
"$jpath" ;;
162 # At this point, the only valid single-character tokens are digits.
163 ''|
[!0-9]) throw
"EXPECTED value GOT ${token:-EOF}" ;;
165 # if asked, replace solidus ("\/") in json strings with normalized value: "/"
166 [ "$NORMALIZE_SOLIDUS" -eq
1 ] && value
=${value//\\\//\/}
168 [ "$value" = '""' ] && isempty
=1
171 [ "$value" = '' ] && return
172 [ "$LEAFONLY" -eq
0 ] && [ "$PRUNE" -eq
0 ] && print
=1
173 [ "$LEAFONLY" -eq
1 ] && [ "$isleaf" -eq
1 ] && [ $PRUNE -eq
0 ] && print
=1
174 [ "$LEAFONLY" -eq
0 ] && [ "$PRUNE" -eq
1 ] && [ "$isempty" -eq
0 ] && print
=1
175 [ "$LEAFONLY" -eq
1 ] && [ "$isleaf" -eq
1 ] && \
176 [ $PRUNE -eq
1 ] && [ $isempty -eq
0 ] && print
=1
177 [ "$print" -eq
1 ] && printf "%s\t%s\n" "${jpath//\"/}" "$value"
187 *) throw
"EXPECTED EOF GOT $token" ;;
191 if ([ "$0" = "$BASH_SOURCE" ] ||
! [ -n
"$BASH_SOURCE" ]);
199 #Copyright (c) 2011 Dominic Tarr
201 #Permission is hereby granted, free of charge,
202 #to any person obtaining a copy of this software and
203 #associated documentation files (the "Software"), to
204 #deal in the Software without restriction, including
205 #without limitation the rights to use, copy, modify,
206 #merge, publish, distribute, sublicense, and/or sell
207 #copies of the Software, and to permit persons to whom
208 #the Software is furnished to do so,
209 #subject to the following conditions:
211 #The above copyright notice and this permission notice
212 #shall be included in all copies or substantial portions of the Software.
214 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
215 #EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
216 #OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
217 #IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
218 #ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
219 #TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
220 #SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.