Skip to content

Instantly share code, notes, and snippets.

@casssiahaluu
Created August 22, 2024 19:16
Show Gist options
  • Save casssiahaluu/7346eed6a5f6dca31c618daeae8942da to your computer and use it in GitHub Desktop.
Save casssiahaluu/7346eed6a5f6dca31c618daeae8942da to your computer and use it in GitHub Desktop.
import json
def load_json(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
return json.load(file)
def collect_keys(data, parent_key=''):
keys = []
for k, v in data.items():
full_key = f"{parent_key}.{k}" if parent_key else k
keys.append(full_key)
if isinstance(v, dict):
keys.extend(collect_keys(v, full_key))
return keys
def compare_keys(all_keys):
all_keys_set = set()
for keys in all_keys.values():
all_keys_set.update(keys)
missing_keys = {}
for file, keys in all_keys.items():
missing_keys[file] = list(all_keys_set - set(keys))
return missing_keys
def main():
file_paths = input("Enter the paths to the JSON files, separated by commas: ").split(',')
all_keys = {}
for file_path in file_paths:
file_path = file_path.strip()
json_data = load_json(file_path)
all_keys[file_path] = collect_keys(json_data)
missing_keys = compare_keys(all_keys)
for file, keys in missing_keys.items():
if keys:
print(f"Missing keys in {file}:")
for key in keys:
print(key)
else:
print(f"No missing keys in {file}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment