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

Jolt转换:为同级数组元素添加指定key并保留其他顶级字段

Jolt转换:保留顶层字段并为数组元素添加公共ID

输入JSON

{
  "some_key": "x",
  "another_key": "y",
  "foo_id": 123,
  "all_foo": [
    {
      "foo_name": "bar1"
    },
    {
      "foo_name": "bar2"
    }
  ]
}

期望输出JSON

{
  "some_key": "x",
  "another_key": "y",
  "all_foo": [
    {
      "foo_id": 123,
      "foo_name": "bar1"
    },
    {
      "foo_id": 123,
      "foo_name": "bar2"
    }
  ]
}

当前尝试的Jolt规则

[
  {
    "operation": "shift",
    "spec": {
      "all_foo": {
        "*": {
          "*": "[&1].&",
          "@(2,foo_id)": "[&1].foo_id"
        }
      }
    }
  }
]

问题:该规则能正确处理all_foo数组,但未对some_key和another_key做映射,导致这两个字段丢失。

修正后的Jolt规则

[
  {
    "operation": "shift",
    "spec": {
      "some_key": "some_key",
      "another_key": "another_key",
      "all_foo": {
        "*": {
          "*": "all_foo[&1].&",
          "@(2,foo_id)": "all_foo[&1].foo_id"
        }
      }
    }
  }
]

规则说明

  • 显式映射some_key和another_key到输出的对应字段,确保顶层字段被保留
  • 处理all_foo数组时:
    • all_foo[&1].&用于保留原数组元素的所有字段(此处为foo_name)
    • @(2,foo_id)通过相对路径引用顶层的foo_id值,将其添加到每个数组元素中
  • 顶层的foo_id未在规则中做映射,因此不会出现在最终输出里,完全匹配期望结果

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 08:23:20