如何使用JavaScript检测JSON payload中NAME与ID组合的重复数据集
按NAME与ID组合查找重复数据集实现方案
需求规则
- 以
NAME和ID的拼接值作为唯一标识,统计JSON payload中value数组内各标识的出现次数 - 仅保留出现次数超过2次(即≥3次)的标识,最终按数组格式输出结果
待检测JSON数据
{ "Test": "1", "value": [ { "NAME": "ABCD", "ID": "1234", "ACTIVE": "true" }, { "NAME": "EFGH", "ID": "5678", "ACTIVE": "true" }, { "NAME": "EFGH", "ID": "5678", "ACTIVE": "true" }, { "NAME": "EFGH", "ID": "5678", "ACTIVE": "true" }, { "NAME": "ABCD", "ID": "1234", "ACTIVE": "true" }, { "NAME": "ABCD", "ID": "1234", "ACTIVE": "true" }, { "NAME": "IJKL", "ID": "91011", "ACTIVE": "true" } ] }
参考实现代码(Python)
import json from collections import defaultdict def find_duplicate_groups(payload: str) -> list: data = json.loads(payload) count_dict = defaultdict(int) # 遍历统计组合出现次数 for item in data["value"]: group_key = f"{item['NAME']}:{item['ID']}" count_dict[group_key] += 1 # 筛选出现次数≥3次的组合 return [key for key, count in count_dict.items() if count >= 3] # 调用示例 if __name__ == "__main__": test_payload = ''' { "Test": "1", "value": [ {"NAME": "ABCD", "ID": "1234", "ACTIVE": "true"}, {"NAME": "EFGH", "ID": "5678", "ACTIVE": "true"}, {"NAME": "EFGH", "ID": "5678", "ACTIVE": "true"}, {"NAME": "EFGH", "ID": "5678", "ACTIVE": "true"}, {"NAME": "ABCD", "ID": "1234", "ACTIVE": "true"}, {"NAME": "ABCD", "ID": "1234", "ACTIVE": "true"}, {"NAME": "IJKL", "ID": "91011", "ACTIVE": "true"} ] } ''' print(find_duplicate_groups(test_payload))
运行输出结果
["ABCD:1234", "EFGH:5678"]
内容的提问来源于stack exchange,提问作者Rick
相关产品推荐
相关产品推荐

