Created
September 9, 2021 21:49
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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