Skip to content

Instantly share code, notes, and snippets.

@brews
Created September 9, 2021 21:49
Show Gist options
  • Save brews/445e8f8009d20df171d2c2b0e9d4334b to your computer and use it in GitHub Desktop.
Save brews/445e8f8009d20df171d2c2b0e9d4334b to your computer and use it in GitHub Desktop.
Demo python script to remove duplicate objects in a JSON file, write output
# Remove duplicate objects in a JSON file.
import json
INPATH = "offending.json"
OUTPATH = "corrected.json"
with open(INPATH, "r") as fl:
json_in = json.load(fl)
# Remove duplicates...
hashables = set(tuple(g.items()) for g in json_in)
# Reinflate to list of dicts...
unique_output = [dict(h) for h in hashables]
with open(OUTPATH, "w") as fl:
json.dump(unique_output, fl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment