A final implementation of JSONPath for Python that aims to be standard compliant, including arithmetic and binary comparison operators and providing clear AST for metaprogramming.
—
JSONPath-NG provides a command-line tool for searching JSON files using JSONPath expressions. The tool can process files or read from stdin, making it useful for scripting and data processing pipelines.
Execute JSONPath queries from the command line against JSON files or stdin input.
def main(*argv):
"""
Main CLI function that processes command line arguments and executes JSONPath queries.
Args:
*argv: Command line arguments (typically sys.argv)
"""
def entry_point():
"""Entry point for the jsonpath_ng console script"""
def find_matches_for_file(expr, f):
"""
Find JSONPath matches in a JSON file.
Args:
expr: Parsed JSONPath expression
f: File object containing JSON data
Returns:
List of DatumInContext objects with matches
"""
def print_matches(matches):
"""
Print JSONPath match results to stdout.
Args:
matches: List of DatumInContext objects
"""The command-line tool is installed automatically with the package:
pip install jsonpath-ngUsage syntax:
jsonpath_ng EXPRESSION [FILES...]Search a single file:
jsonpath_ng '$.users[*].name' data.jsonSearch multiple files:
jsonpath_ng '$.products[*].price' inventory.json catalog.jsonSearch from stdin:
cat data.json | jsonpath_ng '$.users[?(@.age > 18)].name'Using glob patterns:
jsonpath_ng '$.timestamp' logs/*.jsonComplex expressions:
# Find all nested email addresses
jsonpath_ng '$..email' contacts.json
# Union of multiple paths
jsonpath_ng '$.users[*].(name|email)' users.json
# Array slicing
jsonpath_ng '$.results[1:5]' results.jsonThe CLI tool supports standard JSONPath syntax as implemented by jsonpath-ng:
Atomics:
$ - Root object`this` - Current objectOperators:
path1.path2 - Child relationship (like XPath /)path1|path2 - Union of pathspath1..path2 - Recursive descent (somewhere in between)Fields:
fieldname - Field with specific name* - Any field[start:end] - Array slice notation[*] - Any array indexThe tool outputs matching values, one per line:
$ echo '{"users": [{"name": "Alice"}, {"name": "Bob"}]}' | jsonpath_ng '$.users[*].name'
Alice
BobFor complex objects, the entire object is output as JSON:
$ echo '{"users": [{"name": "Alice", "age": 30}]}' | jsonpath_ng '$.users[*]'
{"name": "Alice", "age": 30}The tool handles common error cases:
The CLI tool works well in shell pipelines:
# Count matching results
jsonpath_ng '$.items[*]' data.json | wc -l
# Extract and sort values
jsonpath_ng '$.users[*].name' users.json | sort
# Filter and format results
curl -s api/users | jsonpath_ng '$.data[*].email' | grep "@company.com"
# Pipe to other JSON tools
jsonpath_ng '$.products[*]' catalog.json | jq '.price'The tool is registered as a console script entry point:
# In setup.py
entry_points={
'console_scripts': [
'jsonpath_ng=jsonpath_ng.bin.jsonpath:entry_point'
],
}This makes the jsonpath_ng command available system-wide after installation.
Install with Tessl CLI
npx tessl i tessl/pypi-jsonpath-ng