From a56b59df8c2845f521c8293e1f0ed17132845c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=81=E9=93=83=E8=A7=A3=E6=BB=A2?= <495950007@qq.com> Date: Sat, 8 Jun 2024 23:04:22 +0800 Subject: [PATCH] Add JSON validator function to remove specified keys from JSON strings --- ai_commons/file_processing/json_validator.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/ai_commons/file_processing/json_validator.py b/ai_commons/file_processing/json_validator.py index 13b0a84..39cd288 100644 --- a/ai_commons/file_processing/json_validator.py +++ b/ai_commons/file_processing/json_validator.py @@ -1 +1,17 @@ -# JSON validator module +# Json validator module + +import json + +def remove_keys_from_json(json_string, keys_to_remove): + # 解析JSON字符串为列表 + data_list = json.loads(json_string) + + # 遍历列表中的每个字典 + for data in data_list: + for key_list in keys_to_remove: + for key in key_list: + if key in data: + del data[key] + + # 将列表转换回JSON字符串 + return json.dumps(data_list, ensure_ascii=False) \ No newline at end of file