如何用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
相关产品推荐
相关产品推荐

