Skip to content

Repository files navigation

JSONPath.sh

This is a JSONPath implementation written in Bash.

See how JSONPath.sh compares to other implementations: https://cburgmer.github.io/json-path-comparison/

Currently we rely on GNU implementations of the underlying tools. On OSX you can try wrapping the command like ./ensure_deps.sh ./JSONPath.sh.

Invocation

JSONPath.sh [-[hbjuipwnsSAT]] [-f FILE] [pattern]

-h

Show help text.

-v

Show version information.

-b

Brief output. Only show the values, not the path and key.

-j

Output in JSON format, instead of JSON.sh format.

-u

Strip unnecessary leading path elements.

-i

Case insensitive searching.

-p

Pass JSON.sh formatted data through to the JSON parser only. Useful after JSON.sh data has been manipulated.

-w

Match whole words only (for filter script expression).

-f FILE

Read a FILE instead of reading from standard input.

-n

Do not print header.

-s

Normalize solidus, e.g. convert "\/" to "/".

-S

Print spaces around :'s.

-c

Output JSON in dense/condensed form.

-T

Indent with tabs instead of 4 character spaces.

pattern

the JSONPath query. Defaults to '$.*' if not supplied.

Requirements

JSONPath.sh is a Bash script that uses the standard GNU tools: bash, cat, sed, gawk, grep, and seq. NOTICE that gawk, gnu awk, is required, not mawk or nawk, but gawk.

Installation

Install with pip:

  • pip install JSONPath.sh

Install with npm:

  • sudo npm install -g jsonpath.sh

Install with make:

  • make install

Or copy the JSONPath.sh script to your PATH, for example:

curl -O https://raw.githubusercontent.com/mclarkson/JSONPath.sh/master/JSONPath.sh
chmod +x JSONPath.sh
mv JSONPath.sh ~/bin

Examples

$ ./JSONPath.sh < package.json
["name"]        "JSONPath.sh"
["version"]     "0.0.0"
["description"] "JSONPath implementation written in Bash"
["homepage"]    "http://github.com/mclarkson/JSONPath.sh"
["repository","type"]   "git"
["repository","url"]    "https://github.com/mclarkson/JSONPath.sh.git"
["bin","JSONPath.sh"]   "./JSONPath.sh"
["author"]      "Mark Clarkson <[email protected]>"
["scripts","test"]      "./ensure_deps.sh ./all-tests.sh"

more complex examples:

NPMJS.ORG EXAMPLES

# Number of downloads yesterday
curl -s https://api.npmjs.org/downloads/point/last-day/jsonpath.sh | \
    JSONPath.sh '$.downloads' -b

# Show all versions
curl registry.npmjs.org/express | ./JSONPath.sh '$.versions.*.version'

# Show version 2.2.0
./JSONPath.sh \
    -f test/valid/npmjs.org.json \
    '$.versions.["2.2.0"]'

# Find versions 2.2.x (using a regular expression)
# and show version and contributors
./JSONPath.sh \
    -f test/valid/npmjs.org.json \
    '$..["2.2.*"].[version,contributors]'

JSONPATH.ORG EXAMPLES

# The default query
./JSONPath.sh \
    -f test/valid/jsonpath.com.json \
    '$.phoneNumbers[:1].type'

# The same, but using a filter (script) expression
# (This takes 2 passes through the data)
./JSONPath.sh \
    -f test/valid/jsonpath.com.json \
    '$.phoneNumbers[?(@.type=iPhone)]'

KUBERNETES EXAMPLES

# Show the NodePort of a service named bob
# from a list of all services
kubectl get svc -o json | JSONPath.sh \
    '