Skip to content

Instantly share code, notes, and snippets.

@qsorix
Created July 1, 2015 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qsorix/38f0cadac7dba8e444cb to your computer and use it in GitHub Desktop.
Save qsorix/38f0cadac7dba8e444cb to your computer and use it in GitHub Desktop.
Duplicated json keys
#!/usr/bin/env python
import json
import sys
def list_duplicates(seq):
seen = set()
seen_add = seen.add
# adds all elements it doesn't know yet to seen and all other to seen_twice
seen_twice = set( x for x in seq if x in seen or seen_add(x) )
# turn the set into a list (as requested)
return list( seen_twice )
def conflict(arg):
keys = map(lambda x: x[0], arg)
dups = list_duplicates(keys)
if dups:
print dups
return arg
content = open(sys.argv[1]).read()
json.loads(content, object_pairs_hook=conflict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment