OliveSensorAPI/scripts/merge_json.py

41 lines
1.2 KiB
Python
Raw Normal View History

2024-01-19 15:02:00 +08:00
import json
import os
def save_merge_json(data_lis, file_path):
with open(file_path, 'wt', encoding='utf-8') as file:
2024-01-19 15:52:53 +08:00
json.dump(data_lis, file, indent=4, ensure_ascii=False)
2024-01-19 15:02:00 +08:00
2024-01-23 08:15:01 +08:00
def get_all_file_paths(folder_path, suffix=''):
print(folder_path)
2024-01-19 15:52:53 +08:00
files = os.listdir(folder_path)
path = []
for file in files:
file_path = os.path.join(folder_path, file)
if os.path.isdir(file_path):
path.extend(get_all_file_paths(file_path))
else:
2024-01-23 08:15:01 +08:00
if file_path.endswith(suffix):
path.append(file_path)
2024-01-19 15:52:53 +08:00
return path
2024-01-19 15:02:00 +08:00
if __name__ == '__main__':
conversion_lis = []
2024-01-23 08:15:01 +08:00
folder_path = './' # input
merge_path = 'merge.json' # input
paths = get_all_file_paths(folder_path=folder_path, suffix='.json')
2024-01-19 15:02:00 +08:00
2024-01-19 15:52:53 +08:00
for path in paths:
2024-01-19 15:02:00 +08:00
print(path)
2024-01-19 15:52:53 +08:00
with open(path, 'rt', encoding='utf-8') as lines:
2024-01-23 08:15:01 +08:00
datas = []
2024-01-19 15:52:53 +08:00
for line in lines:
2024-01-23 08:15:01 +08:00
datas.append(line)
try:
datas = json.loads(''.join(datas))
conversion_lis.extend(datas)
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
2024-01-19 15:52:53 +08:00
save_merge_json(data_lis=conversion_lis, file_path=merge_path)