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

JOLT转换JSON日期格式错误求助:需修正日期以匹配输出要求

修正JOLT日期格式转换问题

现有JOLT转换逻辑可完成JSON数据结构转换,但日期格式处理有误,无法将输入中的Timestamp_UTC字段值28-Aug-2023 12:01:00转换为输出要求的28-08-2023 12:01:00格式,需修正JOLT规范中的日期处理逻辑。

输入JSON

{
  "Timestamp_UTC": "28-Aug-2023 12:01:00",
  "Control area - Redox 1_Red Sensor": 439.1354,
  "Measure area - Redox 2_Blue Sensor": 455.73474,
  "Voltage": 13.48,
  "Measure area - PH_1_S/N: 2213805": -0.096,
  "Control area - PH_2_S/N: 2213826_CONTROL": 6.171
}

期望输出JSON

{
  "Date": "28-08-2023 12:01:00",
  "data": [
    {
      "code": "REDOX1",
      "value": 439.1354
    },
    {
      "code": "REDOX2",
      "value": 455.73474
    },
    {
      "code": "PH1",
      "value": -0.096
    },
    {
      "code": "A1-PH2",
      "value": 6.171
    }
  ],
  "stationname": "ftp_device_01"
}

原JOLT问题分析

原JOLT的日期处理逻辑存在两处核心错误:

  • 使用split('/',@(1,DtDatePart))拆分日期,但输入日期的分隔符是-而非/,导致拆分结果完全错误;
  • 未将英文月份缩写(如Aug)转换为两位数字格式(如08),无法匹配目标日期格式要求。

修正后的JOLT规范

[
  {
    "operation": "shift",
    "spec": {
      "Timestamp_UTC": "Date",
      "Control area - Redox 1_Red Sensor": "data.REDOX1",
      "Measure area - Redox 2_Blue Sensor": "data.REDOX2",
      "Measure area - PH_1_S/N: 2213805": "data.PH1",
      "Control area - PH_2_S/N: 2213826_CONTROL": "data.A1-PH2"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "Date": "&",
      "data": {
        "*": {
          "$": "&2[#2].code",
          "@": "&2[#2].value"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "data": {
        "*": {
          "value": [
            "=notNull",
            0
          ]
        }
      },
      // 拆分日期与时间部分
      "DtDateTimeSplit": "=split(' ',@(1,Date))",
      "DtDatePart": "@(1,DtDateTimeSplit[0])",
      "DtTimePart": "@(1,DtDateTimeSplit[1])",
      // 拆分日期的日、月、年
      "DtDateSplit": "=split('-',@(1,DtDatePart))",
      "DtDay": "@(1,DtDateSplit[0])",
      "DtMonthStr": "@(1,DtDateSplit[1])",
      "DtYear": "@(1,DtDateSplit[2])",
      // 将英文月份缩写转为两位数字
      "DtMonthNum": [
        "=replace(@(1,DtMonthStr), 'Jan', '01')",
        "=replace(@(1,&), 'Feb', '02')",
        "=replace(@(1,&), 'Mar', '03')",
        "=replace(@(1,&), 'Apr', '04')",
        "=replace(@(1,&), 'May', '05')",
        "=replace(@(1,&), 'Jun', '06')",
        "=replace(@(1,&), 'Jul', '07')",
        "=replace(@(1,&), 'Aug', '08')",
        "=replace(@(1,&), 'Sep', '09')",
        "=replace(@(1,&), 'Oct', '10')",
        "=replace(@(1,&), 'Nov', '11')",
        "=replace(@(1,&), 'Dec', '12')"
      ],
      // 重组为目标日期格式
      "Date": "=concat(@(1,DtDay),'-',@(1,DtMonthNum),'-',@(1,DtYear),' ',@(1,DtTimePart))"
    }
  },
  {
    "operation": "remove",
    "spec": {
      "Dt*": ""
    }
  },
  {
    "operation": "default",
    "spec": {
      "stationname": "ftp_device_01"
    }
  }
]

修正说明

  1. 先按空格拆分日期与时间,分离出日期段和时间段;
  2. 按-拆分日期段,提取日、英文月份、年三个部分;
  3. 通过链式replace操作,将所有英文月份缩写转换为对应两位数字;
  4. 用concat重组各部分为目标日期格式;
  5. 保留原有的数据结构转换、空值兜底处理和默认字段设置逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 01:38:09