- numpy 형태의 데이터가 들어갈 때 list 로 변경 해 줘야 한다.
import numpy as np
# 예시: NumPy 배열을 리스트로 변환
if isinstance(some_numpy_data, np.ndarray):
data_dict['some_key'] = some_numpy_data.tolist()
def default_converter(o):
if isinstance(o, np.ndarray):
return o.tolist()
raise TypeError(f"Object of type {o.__class__.__name__} is not JSON serializable")
# JSON 저장
with open(f'{HDB_embedding_save_path}.json', 'w') as file:
json.dump(data_dict, file, default=default_converter, indent=4)
from datetime import datetime
import json
def default_converter(o):
if isinstance(o, datetime):
return o.isoformat() # datetime 객체를 ISO 8601 포맷 문자열로 변환
raise TypeError(f"Object of type {o.__class__.__name__} is not JSON serializable")
# 예제 데이터
data = {
"name": "John Doe",
"timestamp": datetime.now()
}
# JSON 파일로 저장
with open('output.json', 'w') as file:
json.dump(data, file, default=default_converter, indent=4)
import json
from bson import ObjectId
def default_converter(o):
if isinstance(o, ObjectId):
return str(o) # ObjectId를 문자열로 변환
raise TypeError(f"Object of type {o.__class__.__name__} is not JSON serializable")
# 예제 데이터: ObjectId 포함
data = {
"name": "John Doe",
"_id": ObjectId()
}
# JSON 파일로 저장
with open('output.json', 'w') as file:
json.dump(data, file, default=default_converter, indent=4)