import glob import json from json import JSONDecodeError from os.path import basename foo_by_file = {} for file in glob.glob('**/*.json', recursive=True): filename = basename(file) with open(file) as file_contents: try: data = json.load(file_contents) foo_by_file[filename] = data.get('foo', 'NOT FOUND') except JSONDecodeError: foo_by_file[filename] = 'INVALID JSON' for file, foo in sorted(foo_by_file.items()): print(f'{file} {foo}')