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

如何用Jolt Spec实现数组元素非空校验及字段赋值与JSON转换?

需求描述

途经点列表(way_points)中仅包含position=1的元素,或同时包含position=1和position=2的元素。转换规则如下:

  • 若存在position=2且预计到达时间(eta)不为null,则messageType赋值为'STOP_ETA_UPDATE';
  • 若不存在position=2,则messageType赋值为'TRACKING_UPDATE'。

输入输出示例

输入JSON1

{
  "task": {
    "external_id": "2023-03-22-13-15-18-184000",
    "way_points": [
      {
        "address": "Warminster Road",
        "position": 1,
        "eta": null
      },
      {
        "address": "7 John Rennie Close",
        "position": 2,
        "eta": "2023-03-22T13:33:54.827Z"
      }
    ]
  },
  "webhook_type": "driver_on_the_way",
  "merchant_id": 60470
}

输出JSON1

{
  "shipper" : 60470,
  "shipmentId" : "externalId",
  "messages" : [ {
    "eta" : "2023-03-22T13:33:54.827Z",
    "messageType" : "STOP_ETA_UPDATE",
    "message" : "driver_on_the_way"
  } ]
}

输入JSON2

{
  "task": {
    "external_id": "2023-03-22-13-15-18-184000",
    "way_points": [
      {
        "address": "Warminster Road",
        "position": 1,
        "eta": null
      }
    ]
  },
  "webhook_type": "driver_on_the_way",
  "merchant_id": 60470
}

输出JSON2

{
  "shipper" : 60470,
  "shipmentId" : "externalId",
  "messages" : [ {
    "eta" : "2023-03-22T13:33:54.827Z",
    "messageType" : "TRACKING_UPDATE",
    "message" : "driver_on_the_way"
  } ]
}

现有Jolt Spec

[
  {
    "operation": "shift",
    "spec": {
      "task": {
        "way_points": {
          "*": {
            "position": {
              "2": {
                "eta": {
                  "|null": null,
                  "#TRACKING_UPDATE": "messageType1",
                  "*": {
                    "#STOP_ETA_UPDATE": "messageType1"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

请完善上述Jolt Spec,实现符合需求的JSON转换逻辑。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 18:23:20