1. import json
  2. import logging
  3. def remove_duplicate_json_objects(json_list, output_file="output.json"):
  4. """
  5. Removes duplicate JSON objects from a list of JSON objects.
  6. Args:
  7. json_list (list): A list of JSON objects (represented as strings).
  8. output_file (str): The file to write the de-duplicated JSON objects to.
  9. Defaults to "output.json".
  10. Returns:
  11. list: A list of unique JSON objects. Returns an empty list on error.
  12. """
  13. logging.basicConfig(level=logging.INFO) #Basic logging
  14. unique_json_objects = []
  15. seen_json = set()
  16. try:
  17. for json_string in json_list:
  18. try:
  19. json_object = json.loads(json_string)
  20. # Convert JSON object to a tuple to make it hashable
  21. json_tuple = tuple(sorted(json_object.items()))
  22. if json_tuple not in seen_json:
  23. unique_json_objects.append(json_object)
  24. seen_json.add(json_tuple)
  25. except json.JSONDecodeError as e:
  26. logging.warning(f"Error decoding JSON: {e}. Skipping object.")
  27. except TypeError as e:
  28. logging.warning(f"Type error processing JSON: {e}. Skipping object.")
  29. except Exception as e:
  30. logging.error(f"Unexpected error processing JSON: {e}. Skipping object.")
  31. except Exception as e:
  32. logging.error(f"An unexpected error occurred: {e}")
  33. return [] #Return empty list on major error
  34. try:
  35. with open(output_file, 'w') as f:
  36. json.dump(unique_json_objects, f, indent=4)
  37. except Exception as e:
  38. logging.error(f"Error writing to file: {e}")
  39. return []
  40. return unique_json_objects

Add your comment