#!/usr/bin/python3 # The MIT License (MIT) # # Copyright © 2020-2025 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. from json import load, dump from sys import argv, exit, stderr, stdin, stdout info = ''' jk [filepath/URI...] Json Keys loads JSON data and finds all its keys non-recursively. Its output is always JSON Lines (JSONL). When the top-level value is an object, the result is a JSON line with the all top-level keys. Top-level arrays are checked non-recursively for objects, and all unique arrays of keys are emitted as JSON Lines, without repeating. ''' # handle standard help cmd-line options, quitting right away in that case if len(argv) > 1 and argv[1] in ('-h', '--h', '-help', '--help'): print(info.strip()) exit(0) def output_line(w, data) -> None: dump(data, w, indent=None, allow_nan=False, separators=(',', ': ')) w.write('\n') def json_keys(w, src, sort_keys) -> None: data = load(src) if isinstance(data, dict): keys = sorted(data.keys()) if sort_keys else data.keys() output_line(w, keys) return if not isinstance(data, (list, tuple)): return got = set() for e in data: if not isinstance(e, dict): continue keys = tuple(e.keys()) if sort_keys: keys = sorted(keys) if keys in got: continue output_line(w, keys) got.add(keys) def seems_url(s: str) -> bool: protocols = ('https://', 'http://', 'file://', 'ftp://', 'data:') return any(s.startswith(p) for p in protocols) args = argv[1:] sort_keys = False sort_opts = ('-s', '--s', '-sort', '--sort', '-sorted', '--sorted') if len(args) > 0 and args[0] in sort_opts: sort_keys = True args = args[1:] try: if len(args) < 1: json_keys(stdout, stdin.buffer, sort_keys) elif len(args) == 1: name = args[0] if name == '-': json_keys(stdout, stdin.buffer, sort_keys) elif seems_url(name): from urllib.request import urlopen with urlopen(name) as inp: json_keys(stdout, inp, sort_keys) else: with open(name, mode='rb') as inp: json_keys(stdout, inp, sort_keys) else: raise ValueError('multiple inputs not allowed') except BrokenPipeError: # quit quietly, instead of showing a confusing error message stderr.close() except KeyboardInterrupt: exit(2) except Exception as e: print(f'\x1b[31m{e}\x1b[0m', file=stderr) exit(1)