3
# License: MIT or Apache
4
# Homepage: http://github.com/dominictarr/JSON.sh
19
echo "Usage: JSON.sh [-b] [-l] [-p] [-s] [-h]"
21
echo "-p - Prune empty. Exclude fields with empty values."
22
echo "-l - Leaf only. Only show leaf nodes, which stops data duplication."
23
echo "-b - Brief. Combines 'Leaf only' and 'Prune empty' options."
24
echo "-n - No-head. Do not show nodes that have no path (lines that start with [])."
25
echo "-s - Remove escaping of the solidus symbol (straight slash)."
26
echo "-h - This help text."
33
while [ "$ARGN" -ne 0 ]
49
-s) NORMALIZE_SOLIDUS=1
51
?*) echo "ERROR: Unknown option."
63
local pattern_string=$1
67
start=match($0, pattern);
68
token=substr($0, start, RLENGTH);
70
$0=substr($0, start+RLENGTH);
72
}' pattern="$pattern_string"
80
if echo "test string" | grep -E -ao --color=never "test" >/dev/null 2>&1
82
GREP='grep -E -ao --color=never'
87
if echo "test string" | grep -E -o "test" >/dev/null 2>&1
89
ESCAPE='(\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'
90
CHAR='[^[:cntrl:]"\\]'
93
ESCAPE='(\\\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'
94
CHAR='[^[:cntrl:]"\\\\]'
97
local STRING="\"$CHAR*($ESCAPE$CHAR*)*\""
98
local NUMBER='-?(0|[1-9][0-9]*)([.][0-9]*)?([eE][+-]?[0-9]*)?'
99
local KEYWORD='null|false|true'
100
local SPACE='[[:space:]]+'
102
# Force zsh to expand $A into multiple words
103
local is_wordsplit_disabled=$(unsetopt 2>/dev/null | grep -c '^shwordsplit$')
104
if [ $is_wordsplit_disabled != 0 ]; then setopt shwordsplit; fi
105
$GREP "$STRING|$NUMBER|$KEYWORD|$SPACE|." | grep -E -v "^$SPACE$"
106
if [ $is_wordsplit_disabled != 0 ]; then unsetopt shwordsplit; fi
118
parse_value "$1" "$index"
125
*) throw "EXPECTED , or ] GOT ${token:-EOF}" ;;
131
[ "$BRIEF" -eq 0 ] && value=$(printf '[%s]' "$ary") || value=
145
'"'*'"') key=$token ;;
146
*) throw "EXPECTED string GOT ${token:-EOF}" ;;
151
*) throw "EXPECTED : GOT ${token:-EOF}" ;;
154
parse_value "$1" "$key"
155
obj="$obj$key:$value"
160
*) throw "EXPECTED , or } GOT ${token:-EOF}" ;;
166
[ "$BRIEF" -eq 0 ] && value=$(printf '{%s}' "$obj") || value=
171
local jpath="${1:+$1,}$2" isleaf=0 isempty=0 print=0
173
'{') parse_object "$jpath" ;;
174
'[') parse_array "$jpath" ;;
175
# At this point, the only valid single-character tokens are digits.
176
''|[!0-9]) throw "EXPECTED value GOT ${token:-EOF}" ;;
178
# if asked, replace solidus ("\/") in json strings with normalized value: "/"
179
[ "$NORMALIZE_SOLIDUS" -eq 1 ] && value=$(echo "$value" | sed 's#\\/#/#g')
181
[ "$value" = '""' ] && isempty=1
184
[ "$value" = '' ] && return
185
[ "$NO_HEAD" -eq 1 ] && [ -z "$jpath" ] && return
187
[ "$LEAFONLY" -eq 0 ] && [ "$PRUNE" -eq 0 ] && print=1
188
[ "$LEAFONLY" -eq 1 ] && [ "$isleaf" -eq 1 ] && [ $PRUNE -eq 0 ] && print=1
189
[ "$LEAFONLY" -eq 0 ] && [ "$PRUNE" -eq 1 ] && [ "$isempty" -eq 0 ] && print=1
190
[ "$LEAFONLY" -eq 1 ] && [ "$isleaf" -eq 1 ] && \
191
[ $PRUNE -eq 1 ] && [ $isempty -eq 0 ] && print=1
192
[ "$print" -eq 1 ] && printf "[%s]\t%s\n" "$jpath" "$value"
202
*) throw "EXPECTED EOF GOT $token" ;;
206
if ([ "$0" = "$BASH_SOURCE" ] || ! [ -n "$BASH_SOURCE" ]);
212
# vi: expandtab sw=2 ts=2