import json # Open the input and output files with open('/home/yiyangai/stephenqs/datasets/wikihow/WikihowText_data.json', 'r', encoding='utf-8') as infile, open('/home/yiyangai/stephenqs/datasets/wikihow/Computers and Electronics.json', 'w', encoding='utf-8') as outfile: for line in infile: try: # Parse the JSON object from the current line item = json.loads(line.strip()) # Apply filtering condition if item.get("category_hierarchy") and item["category_hierarchy"][0] == "Computers and Electronics": # Write the JSON object as a single line to the output file outfile.write(json.dumps(item, ensure_ascii=False) + '\n') except json.JSONDecodeError as e: print(f"Skipping invalid JSON object: {e}") print("Extraction complete.")