import json import sys # Check if the correct number of arguments is provided if len(sys.argv) != 3: print("Usage: python filter_sort_unique_json.py ") sys.exit(1) # Assign command-line arguments to variables input_file_path = sys.argv[1] output_file_path = sys.argv[2] # Initialize a list to hold valid JSON objects json_objects = [] # Open the input file and read lines with open(input_file_path, 'r') as input_file: for line in input_file: try: # Try to parse the line as JSON json_object = json.loads(line) # If successful, add the JSON object to the list json_objects.append(json_object) except json.JSONDecodeError: # If JSON parsing fails, skip the line continue # Sort the list of JSON objects by the 'Timestamp' field json_objects_sorted = sorted(json_objects, key=lambda x: x['Timestamp']) # Remove duplicates by 'Timestamp' using a dictionary to preserve order json_objects_unique = {} for obj in json_objects_sorted: json_objects_unique[obj['Timestamp']] = obj # Write the unique JSON objects to the output file with open(output_file_path, 'w') as output_file: for timestamp, obj in json_objects_unique.items(): json_line = json.dumps(obj) output_file.write(json_line + '\n') print(f"Processing complete. Sorted and unique records written to {output_file_path}")