import json | |
def keep_only_one_source_domain(json_file_path): | |
with open(json_file_path, 'r') as file: | |
data = json.load(file) | |
seen_domains = set() | |
unique_data = [] | |
for item in data: | |
source_domain = item.get('source_domain') | |
json_obj = {} | |
json_obj["url"] = item["url"] | |
json_obj["source_domain"] = item["source_domain"] | |
json_obj["title"] = item["title"] | |
json_obj["raw_content"] = " ".join(item["raw_content"].split()[:50]) | |
if source_domain not in seen_domains: | |
seen_domains.add(source_domain) | |
unique_data.append(json_obj) | |
with open("unique_source_documents.json", 'w') as file: | |
json.dump(unique_data, file, indent=4, ensure_ascii=False) | |
# Usage example: | |
json_file_path = 'selected_documents.json' # Replace 'data.json' with the path to your JSON file | |
keep_only_one_source_domain(json_file_path) | |