Skip to content

Commit a9f5792

Browse files
committed
With no options default query to '$.*'. Nameless array fix + tests
1 parent 1209f75 commit a9f5792

10 files changed

+3100
-5
lines changed

JSONPath.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ usage() {
103103
# ---------------------------------------------------------------------------
104104

105105
echo
106-
echo "Usage: JSONPath.sh [-n] [-s] [-b] [j] [-h] [-f FILE] pattern"
106+
echo "Usage: JSONPath.sh [-n] [-s] [-b] [j] [-h] [-f FILE] [pattern]"
107107
echo
108-
echo "pattern - the JSON Path query, e.g. \"$..\""
108+
echo "pattern - the JSONPath query. Defaults to '$.*' if not supplied."
109109
echo "-s - Remove escaping of the solidus symbol (straight slash)."
110110
echo "-b - Brief. Only show values."
111111
echo "-j - JSON ouput."
@@ -156,6 +156,7 @@ parse_options() {
156156
shift 1
157157
ARGN=$((ARGN-1))
158158
done
159+
[[ -z $QUERY ]] && QUERY='$.*'
159160
}
160161

161162
# ---------------------------------------------------------------------------
@@ -378,6 +379,8 @@ create_filter() {
378379
a=${a%]}
379380
if [[ $a =~ [[:alpha:]] ]]; then
380381
a=$(echo $a | sed -r "s/[\"']//g;s/([^,]*)/\"\1\"/g")
382+
else
383+
[[ $i -gt 0 ]] && comma=","
381384
fi
382385
#idx=$(echo "${PATHTOKENS[i]}" | tr -d "[]")
383386
query+="$comma$a"

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ yo, so it's a JSONPath implementation written in Bash - and it probably only wor
66

77
## Invocation
88

9-
JSONPath.sh [-n] [-s] [-b] [-i] [-j] [-h] [-p] [-u] [-f FILE] pattern
9+
JSONPath.sh [-n] [-s] [-b] [-i] [-j] [-h] [-p] [-u] [-f FILE] [pattern]
1010

1111
pattern
12-
> the JSONPath query
12+
> the JSONPath query. Defaults to '$.\*' if not supplied.
1313
1414
-b
1515
> Brief output. Only show the values, not the path and key.
@@ -71,7 +71,7 @@ mv JSONPath.sh ~/bin
7171
## Examples
7272

7373
``` bash
74-
$ ./JSONPath.sh '$.*' < package.json
74+
$ ./JSONPath.sh < package.json
7575
["name"] "JSONPath.sh"
7676
["version"] "0.0.0"
7777
["description"] "JSONPath implementation written in Bash"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$..[0]..metadata..["owner.*","name"]

0 commit comments

Comments
 (0)