如何用Jolt将JSON中runtime的时分秒格式转为整数秒数
Jolt转换实现时分秒格式runtime转秒数及字段映射
以下是满足需求的Jolt转换规范,可同时完成runtime的秒数转换、caption的布尔值转换以及其他字段的平级映射:
[ { "operation": "shift", "spec": { "title_info": { "titleId": "titleId", "contentType": "contentType", "caption": { "Yes": "caption" }, "runtime": "runtime" } } }, { "operation": "modify-overwrite-beta", "spec": { "caption": "=toBoolean", "runtime": "=intSum(@(1,runtime).split(':')[0]*3600, @(1,runtime).split(':')[1]*60, @(1,runtime).split(':')[2])" } } ]
转换逻辑说明:
- Shift操作:将
title_info下的指定字段提取到根节点,同时对caption做初步映射,指定"Yes"对应目标字段。 - Modify操作:
caption:通过=toBoolean内置函数将字符串"Yes"转换为布尔值true。runtime:先通过split(':')将时分秒格式的字符串分割为数组,分别取出小时、分钟、秒对应的元素,按小时*3600 + 分钟*60 + 秒的公式计算总秒数,最后通过intSum转为整数类型。
输入示例:
{ "title_info": { "titleId": "3518864", "contentType": "Episode", "titleBrief": "Beirut S1: 04", "runtime": "01:45:23", "caption": "Yes" } }
输出结果:
{ "titleId": "3518864", "contentType": "Episode", "caption": true, "runtime": 6323 }
内容的提问来源于stack exchange,提问作者user1111880
相关产品推荐
相关产品推荐

