-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathprocess_json_files.py
More file actions
60 lines (48 loc) · 2.21 KB
/
Copy pathprocess_json_files.py
File metadata and controls
60 lines (48 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import json
import os
import re
def process_json_files(directory):
# Loop through all files in the specified directory
for filename in os.listdir(directory):
if filename.endswith(".json"):
print(filename)
file_path = os.path.join(directory, filename)
output_file_path = os.path.join(directory, f"post_{filename}")
# print(output_file_path)
with open(file_path, 'r') as file:
content = file.read()
# Try to extract JSON using the regex for embedded JSON as before
matches = re.findall(r'```json\n({.*?})\n```', content, re.DOTALL)
if matches:
# If match found, parse and save the JSON
json_data = json.loads(matches[0])
else:
# If no match, parse the entire file content as JSON
json_data = json.loads(content)
if isinstance(json_data, dict):
new_json_data = list(json_data.values())
if len(new_json_data) <= 1:
json_data = new_json_data[0]
else:
json_data = json_data
def process_nested_json(json_data):
processed_json = {}
# Check if the item is a dictionary and process accordingly
for key, value in json_data.items():
processed_list = [topic.replace(
"/", " or ") for topic in value]
processed_json[key] = processed_list
return processed_json
final = process_nested_json(json_data)
with open(output_file_path, 'w') as json_file:
# json.dump(processed_json, json_file, indent=4)
json.dump(final, json_file, indent=4)
print(f"Processed {filename} -> {output_file_path}")
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
description='Process topics JSON files in a directory')
parser.add_argument('--topics_dir', type=str, default='./data/topics/',
help='Directory of topics JSON files to process')
args = parser.parse_args()
process_json_files(args.topics_dir)