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

使用JOLT拆解嵌套动态JSON数组,生成SQL适配格式

使用JOLT将可变结构的JSON展平为SQL适用格式

原始JSON

{
  "ProductLine": [
    "Product 1",
    "Product 2"
  ],
  "Purchase": 364,
  "Cancel": [
    140,
    2
  ]
}

当前使用的JOLT转换规则

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "@": "[#2].&2"
        }
      }
    }
  }
]

期望输出

[
  {
    "ProductLine": "Product 1",
    "Purchase": 364,
    "Cancel": 140
  },
  {
    "ProductLine": "Product 2",
    "Cancel": 2
  }
]

核心难点

  • 数组结构可变:Cancel字段有时是数组,有时可能是单个值
  • Purchase模块存在嵌套可能性,需要兼容不同层级结构

解决方案:适配可变结构的JOLT规则

针对可变结构的展平需求,可通过分阶段处理实现:先统一数组/单值字段格式,再按索引对齐合并。

最终JOLT规则

[
  // 第一步:统一字段格式,为数组元素记录索引
  {
    "operation": "shift",
    "spec": {
      "ProductLine": {
        "*": {
          "@": "[&1].ProductLine",
          "$": "[&1].index"
        }
      },
      "Purchase": {
        "@": "[0].Purchase"
      },
      "Cancel": {
        "*": {
          "@": "[&1].Cancel",
          "$": "[&1].index"
        }
      }
    }
  },
  // 第二步:按索引合并对象
  {
    "operation": "shift",
    "spec": {
      "*": {
        "index": {
          "@(2)": "[&1].&"
        }
      }
    }
  },
  // 第三步:移除临时索引字段
  {
    "operation": "remove",
    "spec": {
      "*": {
        "index": ""
      }
    }
  }
]

规则说明

  1. 格式统一阶段:把单值字段转为数组格式,为数组元素添加索引标记,确保后续能按索引对齐。
  2. 合并对齐阶段:依据索引字段将不同属性的元素合并到同一对象中,自动补全对应索引的字段值。
  3. 清理阶段:删除临时生成的索引字段,得到最终扁平化结构。

该规则兼容以下场景:

  • Cancel为单值时,自动匹配到索引0的对象
  • Purchase为嵌套结构时,仅需调整第一步中Purchase的匹配路径即可适配

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 12:40:31