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

JOLT转换生成数组求助:动态primaryID场景输出异常

JOLT转换解决方案:修复动态ID的Number字段缺失问题

输入JSON

{
  "primaryID": "DNI",
  "DNI": 123,
  "CUIL": 345
}

期望输出

{
  "NationalIds": [
    {
      "Type": "CUIL",
      "Number": 345
    },
    {
      "Type": "DNI",
      "isPrimary": "true",
      "Number": 123
    }
  ]
}

问题分析

原转换规则存在两个核心问题:

  1. 第二步硬编码了CUIL和DNI字段,无法支持动态ID类型;
  2. 第四步对Number字段的处理逻辑嵌套错误,仅处理了与primaryID匹配的项,导致其他ID的Number丢失。

正确JOLT转换规则

[
  {
    "operation": "shift",
    "spec": {
      "primaryID": "primaryID",
      "*": "ids.&"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "ids": {
        "*": {
          "$": "NationalIds[].Type",
          "@": "NationalIds[&1].Number",
          "@(2,primaryID)": {
            "$": {
              "$2": {
                "#true": "NationalIds[&3].isPrimary"
              }
            }
          }
        }
      }
    }
  }
]

规则解释

  1. 第一步Shift操作:

    • 保留primaryID字段用于后续判断;
    • 将所有非primaryID的字段统一收集到ids对象下,支持任意动态ID类型,无需硬编码。
  2. 第二步Shift操作:

    • 遍历ids中的每个字段,为每个ID生成NationalIds数组元素:
      • $:将当前字段名(如CUIL、DNI)赋值给Type字段;
      • @:将当前字段值(如345、123)赋值给对应数组元素的Number字段;
      • 通过@(2,primaryID)获取顶层primaryID值,对比当前Type是否与primaryID匹配,匹配则添加isPrimary: true标记。

内容的提问来源于stack exchange,提问作者Maite Reffle

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 12:25:14