import json
import logging
def remove_duplicate_json_objects(json_list, output_file="output.json"):
"""
Removes duplicate JSON objects from a list of JSON objects.
Args:
json_list (list): A list of JSON objects (represented as strings).
output_file (str): The file to write the de-duplicated JSON objects to.
Defaults to "output.json".
Returns:
list: A list of unique JSON objects. Returns an empty list on error.
"""
logging.basicConfig(level=logging.INFO) #Basic logging
unique_json_objects = []
seen_json = set()
try:
for json_string in json_list:
try:
json_object = json.loads(json_string)
# Convert JSON object to a tuple to make it hashable
json_tuple = tuple(sorted(json_object.items()))
if json_tuple not in seen_json:
unique_json_objects.append(json_object)
seen_json.add(json_tuple)
except json.JSONDecodeError as e:
logging.warning(f"Error decoding JSON: {e}. Skipping object.")
except TypeError as e:
logging.warning(f"Type error processing JSON: {e}. Skipping object.")
except Exception as e:
logging.error(f"Unexpected error processing JSON: {e}. Skipping object.")
except Exception as e:
logging.error(f"An unexpected error occurred: {e}")
return [] #Return empty list on major error
try:
with open(output_file, 'w') as f:
json.dump(unique_json_objects, f, indent=4)
except Exception as e:
logging.error(f"Error writing to file: {e}")
return []
return unique_json_objects
Add your comment