Skip to content

Instantly share code, notes, and snippets.

@APN-Pucky
Created May 19, 2022 08:36
Show Gist options
  • Save APN-Pucky/06af24891bc8d27e91e87be29cbb5bc8 to your computer and use it in GitHub Desktop.
Save APN-Pucky/06af24891bc8d27e91e87be29cbb5bc8 to your computer and use it in GitHub Desktop.
Python script to merge jsons with duplicate indices of dicts
import json
import sys
def myhook(pairs):
d = {}
for k, v in pairs:
if k not in d:
d[k] = v
else:
d[k] = {**d[k],**v}
return d
def main(argv):
for f in argv[1:]:
mydata = json.load(open(f), object_pairs_hook=myhook)
open(f,'w').write( json.dumps(mydata))
if __name__ == "__main__":
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment