You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 17:27:02