#!/usr/bin/env python3 """ Clean JSON files by removing the raw_result field """ import json import sys from pathlib import Path def clean_json_file(json_file): """Remove raw_result field from JSON file""" json_path = Path(json_file) if not json_path.exists(): print(f"❌ File not found: {json_file}") return False try: # Read JSON with open(json_path, 'r', encoding='utf-8') as f: data = json.load(f) # Remove raw_result if it exists removed = False if isinstance(data, dict) and 'data' in data: if 'raw_result' in data['data']: del data['data']['raw_result'] removed = True # Save cleaned JSON with open(json_path, 'w', encoding='utf-8') as f: json.dump(data, f, indent=2, ensure_ascii=False) if removed: print(f"✅ Cleaned: {json_path.name} (removed raw_result)") else: print(f"ℹ️ No raw_result found in: {json_path.name}") return True except Exception as e: print(f"❌ Error processing {json_path.name}: {e}") return False def clean_directory(directory): """Clean all JSON files in a directory""" dir_path = Path(directory) if not dir_path.exists(): print(f"❌ Directory not found: {directory}") return json_files = list(dir_path.glob("*.json")) if not json_files: print(f"❌ No JSON files found in: {directory}") return print(f"🔍 Found {len(json_files)} JSON files") for json_file in json_files: clean_json_file(json_file) def main(): if len(sys.argv) < 2: print("Usage: python clean_json.py ") print("Example: python clean_json.py output.json") print(" python clean_json.py /path/to/directory/") sys.exit(1) target = sys.argv[1] target_path = Path(target) if target_path.is_file(): clean_json_file(target) elif target_path.is_dir(): clean_directory(target) else: print(f"❌ Invalid target: {target}") sys.exit(1) if __name__ == "__main__": main()