import json with open("input") as f: document = f.readline().strip() document = json.loads(document) def iterate_object(json_object): object_sum = 0 object_type = type(json_object) if object_type == list: for item in json_object: object_sum += iterate_object(item) elif object_type == int: return json_object elif object_type == str: pass elif object_type == dict: for item in json_object.values(): if item == "red": return 0 object_sum += iterate_object(item) else: raise TypeError(f"No case for type {object_type}!") return object_sum print(iterate_object(document))