mode = os.fstat(0).st_mode if not stat.S_ISFIFO(mode) and not stat.S_ISREG(mode): # input is terminal, not what we want print("Redirect data to stdin, please (e.g. with a pipe)." " Run with -h for usage.") sys.exit(1) try: json_data = json.loads(sys.stdin.read()) except Exception: print("Could not read json data from stdin") sys.exit(1) # We'll either get a dict or a list... if type(json_data) == dict: # ... and just make it a list. json_data = [json_data] # All keys on JSON data should be strings. keys = set() for item in json_data: # We assume only dictionaries here keys.update(list(item.keys())) keys = sorted(list(keys)) if args.human: ui_values_to_columns(keys) else: print(json.dumps(keys))
"""List distinct keys used on JSON objects. Usage: python list_keys.py [--json] """ from __future__ import print_function import sys import os import json from util import import_data, ui_import_data, ui_values_to_columns, distinct_keys if __name__ == "__main__": if len(sys.argv) > 1 and sys.argv[1] == "--json": # Assume raw data might be piped, no frills. json_data, _ = import_data() if json_data: print(json.dumps(list(distinct_keys(json_data)))) else: sys.exit(1) else: # Default behavior, assuming human consumption. print("Printing distinct list of keys.") json_data = ui_import_data() all_keys = distinct_keys(json_data) print("%s keys found in all the blobs:\n" % len(all_keys)) ui_values_to_columns(all_keys)
search_key = sys.argv[1] where_key = None where_value = None json_data = ui_import_data() stats, num_matches = value_counter(json_data, search_key, where_key, where_value) if not stats: print("Sorry, didn't find any stats for '%s' in the JSON." % search_key) sys.exit(1) title = "List of values from field '%s'" % search_key print("\n\n%s" % title) print("(Data from %s out of %s blobs)" % (num_matches, len(json_data))) print("-" * len(title)) ui_values_to_columns(sorted(stats.keys())) elif len(sys.argv) == 3 and sys.argv[2] == "--json": # Count values associated with key, machine output. search_key = sys.argv[1] where_key = None where_value = None json_data = import_data()[0] stats, num_matches = value_counter(json_data, search_key, where_key, where_value) if not stats: # Still JSON parser friendly, indicator of fail with emptiness. print(json.dumps([])) sys.exit(1) else: print(json.dumps(sorted(stats.keys()))) elif len(sys.argv) == 4:
where_value = None json_data = ui_import_data() stats, num_matches = value_counter(json_data, search_key, where_key, where_value) if not stats: print("Sorry, didn't find any stats for '%s' in the JSON." % search_key) sys.exit(1) title = "List of values from field '%s'" % search_key print("\n\n%s" % title) print("(Data from %s out of %s blobs)" % (num_matches, len(json_data))) print("-" * len(title)) ui_values_to_columns(sorted(stats.keys())) elif len(sys.argv) == 3 and sys.argv[2] == "--json": # Count values associated with key, machine output. search_key = sys.argv[1] where_key = None where_value = None json_data = import_data()[0] stats, num_matches = value_counter(json_data, search_key, where_key, where_value) if not stats: # Still JSON parser friendly, indicator of fail with emptiness. print(json.dumps([])) sys.exit(1) else: print(json.dumps(sorted(stats.keys())))
# http://stackoverflow.com/questions/13442574/how-do-i-determine-if-sys-stdin-is-redirected-from-a-file-vs-piped-from-another mode = os.fstat(0).st_mode if not stat.S_ISFIFO(mode) and not stat.S_ISREG(mode): # input is terminal, not what we want print("Redirect data to stdin, please (e.g. with a pipe). -h for usage.") sys.exit(1) try: json_data = json.loads(sys.stdin.read()) except Exception: print("Could not read json data from stdin") sys.exit(1) # We'll either get a dict or a list... if type(json_data) == dict: # ... and just make it a list. json_data = [json_data] # All keys on JSON data should be strings. keys = set() for item in json_data: # We assume only dictionaries here keys.update(item.keys()) keys = sorted(list(keys)) if args.human: ui_values_to_columns(keys) else: print(json.dumps(keys))