I've been looking at walls of JSON for the last few days, and realized that I really shouldn't be doing that. So I made a script that will continuously indent and output a stream of JSON data formatted and indented properly.

import json
import sys
import argparse

arguments = argparse.ArgumentParser(description='Prettify JSON streams and optionally remove certain keys')
arguments.add_argument('--filter-keys', nargs='+')

def is_valid_json(text):
    try:
        json_object = json.loads(text)
    except ValueError as e:
        return False
    return True

if __name__ == "__main__":
    args = arguments.parse_args()
    stream = ''

    try:
        while True:
            line = sys.stdin.readline()
            stream += line
            if is_valid_json(stream):
                data = json.loads(stream)

                if args.filter_keys:
                    for key, val in data.items():
                        if key in args.filter_keys:
                            del data[key]

                print(json.dumps(data, indent=4))
                stream = ''
            else:
                print("invalid json")
    except KeyboardInterrupt:
        sys.exit(0)

You can filter out root-level keys with a list of arguments to --filter-keys, and it will not display the data from those keys.

tags: python json streams