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

如何使用JOLT按站点统计JSON记录数并生成指定汇总结果

需求说明

需基于给定的JSON文件,按站点生成汇总信息。

输入JSON

[
  {
    "time": "2024-03-25T00:00:00Z",
    "station": "1",
    "temp": "2.0",
    "rain": "1.6"
  },
  {
    "time": "2024-03-25T01:00:00Z",
    "station": "1",
    "temp": "2.6",
    "rain": "2.1"
  },
  {
    "time": "2024-03-25T01:00:00Z",
    "station": "2",
    "temp": "2.6",
    "rain": "2.1"
  },
  {
    "time": "2024-03-25T01:00:00Z",
    "station": "3",
    "temp": "2.6",
    "rain": "2.1"
  }
]

期望输出

[
  {
    "sid": "1",
    "cnt": 2
  },
  {
    "sid": "2",
    "cnt": 1
  },
  {
    "sid": "3",
    "cnt": 1
  }
]

当前问题

参考相关内容编写了如下JOLT转换代码:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "station": {
          "@(2,[&1])": "@(1).[]"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "cnt": "=size(@(2))",
          "id": "=toInteger"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

但输出结果不符合预期,实际输出为:

[
  {
    "time": "2024-03-25T00:00:00Z",
    "station": "1",
    "temp": "2.0",
    "rain": "1.6",
    "cnt": 2
  },
  {
    "time": "2024-03-25T01:00:00Z",
    "station": "1",
    "temp": "2.6",
    "rain": "2.1",
    "cnt": 2
  },
  {
    "time": "2024-03-25T01:00:00Z",
    "station": "2",
    "temp": "2.6",
    "rain": "2.1",
    "cnt": 1
  },
  {
    "time": "2024-03-25T01:00:00Z",
    "station": "3",
    "temp": "2.6",
    "rain": "2.1",
    "cnt": 1
  }
]

请问如何通过JOLT实现符合期望的转换?


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 09:06:01