Created
March 15, 2024 05:40
-
-
Save shinkou/3f718a797f8809c0b69295a63bbeafc0 to your computer and use it in GitHub Desktop.
JMESPath Evaluator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# vim: fileencoding=utf-8 | |
import argparse, jmespath, json, sys | |
def get_args(): | |
parser = argparse.ArgumentParser(description='JMESPath Evaluator') | |
parser.add_argument('-i', '--indent', type=int, help='indent') | |
parser.add_argument( | |
'--ensure-ascii' | |
, action='store_true' | |
, help='ensure non-ASCII character escape' | |
) | |
parser.add_argument('jpath', metavar='PATH', help='JMESPath') | |
parser.add_argument( | |
'filepaths' | |
, metavar='FILE' | |
, nargs='+' | |
, help='file path' | |
) | |
return parser.parse_args() | |
def main(): | |
args = get_args() | |
jparsed = jmespath.compile(args.jpath) | |
for fpath in args.filepaths: | |
with open(fpath, 'r') as f: | |
print( | |
json.dumps( | |
jparsed.search(json.loads(f.read())) | |
, indent=args.indent | |
, ensure_ascii=args.ensure_ascii | |
) | |
) | |
if '__main__' == __name__: | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment