Skip to content

Instantly share code, notes, and snippets.

@mesbahamin
Last active July 28, 2016 21:44
Show Gist options
  • Save mesbahamin/b8f1d4ef2cc6dfed2608f45281bc304e to your computer and use it in GitHub Desktop.
Save mesbahamin/b8f1d4ef2cc6dfed2608f45281bc304e to your computer and use it in GitHub Desktop.
{
"key":1234,
"key":5678,
"key":"abcd",
"bork":"asdf",
"bork":"dfsd"
}
"""Example Output:
$ python detect_duplicate_keys.py
Traceback (most recent call last):
File "detect_duplicate_keys.py", line 43, in <module>
print(detect_duplicates(json_file))
File "detect_duplicate_keys.py", line 36, in detect_duplicates
duplicate_keys, json_file
__main__.DuplicateKeysError: Duplicate keys(s): ['key', 'bork'] in json file: duplicate_keys.json
"""
import collections
import json
import pathlib
class DuplicateKeysError(Exception):
pass
def detect_duplicates(json_file):
"""Raise exception if json_file contains multiple identical keys."""
with json_file.open('r') as f:
d = json.load(f, object_pairs_hook=list)
keys = [key_value_pair[0] for key_value_pair in d]
duplicate_keys = [
key for key,count
in collections.Counter(keys).items()
if count > 1
]
if len(duplicate_keys) != 0:
raise DuplicateKeysError(
"Duplicate key(s): {} in json file: {}".format(
duplicate_keys, json_file
)
)
if __name__ == '__main__':
json_file = pathlib.Path('.', 'duplicate_keys.json')
detect_duplicates(json_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment