import json | |
filtered_data = [] | |
# Load the JSON file content as a single string | |
with open('/home/yiyangai/stephenqs/datasets/wikihow/WikihowText_data.json', 'r', encoding='utf-8') as f: | |
content = f.read() | |
# Split the content into individual JSON objects | |
json_objects = content.splitlines() | |
# Process each JSON object individually | |
for obj in json_objects: | |
try: | |
item = json.loads(obj.strip()) | |
if item.get("category_hierarchy") and item["category_hierarchy"][0] == "Finance and Business": | |
filtered_data.append(item) | |
except json.JSONDecodeError as e: | |
print(f"Skipping invalid JSON object: {e}") | |
# Save the filtered data to a new JSON file | |
with open('Arts and Entertainment.json', 'w', encoding='utf-8') as f: | |
json.dump(filtered_data, f, ensure_ascii=False, indent=4) | |
print(f"{len(filtered_data)} items extracted to 'output.json'") | |