Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save guilhermecgs/9ab3d0ba8478a37bdaf4d4d9456d7a01 to your computer and use it in GitHub Desktop.
Save guilhermecgs/9ab3d0ba8478a37bdaf4d4d9456d7a01 to your computer and use it in GitHub Desktop.
comparador de chaves de json
import json
def compare_json_keys(file1, file2):
def get_keys(d, parent_key=''):
keys = []
for k, v in d.items():
new_key = f"{parent_key}.{k}" if parent_key else k
if isinstance(v, dict):
keys.extend(get_keys(v, new_key))
else:
keys.append(new_key)
return keys
with open(file1, 'r') as f:
json1 = json.load(f)
with open(file2, 'r') as f:
json2 = json.load(f)
keys1 = set(get_keys(json1))
keys2 = set(get_keys(json2))
only_in_json1 = keys1 - keys2
only_in_json2 = keys2 - keys1
return only_in_json1, only_in_json2
# Exemplo de uso
file1 = '/(...)/src/context/i18n/locales/pt.json'
file2 = '/(...)/src/context/i18n/locales/en.json'
only_in_json1, only_in_json2 = compare_json_keys(file1, file2)
print("Chaves presentes apenas no pt-BR JSON:")
for key in only_in_json1:
print(key)
print("\nChaves presentes apenas no en-US JSON:")
for key in only_in_json2:
print(key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment