Skip to content

Commit

Permalink
feat: update json_validator to support flexible timestamp field names
Browse files Browse the repository at this point in the history
  • Loading branch information
sealbell committed Jun 10, 2024
1 parent ce5ec74 commit c045e6b
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion ai_commons/file_processing/json_validator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Json validator module

import json
from datetime import datetime

def remove_keys_from_json(json_string, keys_to_remove):
# 解析JSON字符串为列表
Expand All @@ -14,4 +15,31 @@ def remove_keys_from_json(json_string, keys_to_remove):
del data[key]

# 将列表转换回JSON字符串
return json.dumps(data_list, ensure_ascii=False)
return json.dumps(data_list, ensure_ascii=False)

import json
from datetime import datetime

def change_timestamp_format(json_string, timestamp_field):
"""
将JSON字符串中的指定字段的时间戳格式从 'YYYY-MM-DD HH:MM:SS' 改为 'YYYY-MM-DD'。
Args:
json_string (str): 输入的JSON字符串。
timestamp_field (str): 需要转换时间戳格式的字段名。
Returns:
str: 修改后的JSON字符串。
"""
data_list = json.loads(json_string)

for data in data_list:
if timestamp_field in data:
try:
original_timestamp = data[timestamp_field]
new_timestamp = datetime.strptime(original_timestamp, '%Y-%m-%d %H:%M:%S').strftime('%Y-%m-%d')
data[timestamp_field] = new_timestamp
except ValueError:
pass

return json.dumps(data_list, ensure_ascii=False)

0 comments on commit c045e6b

Please sign in to comment.