如何通过JOLT转换将关联文本与链接合并为对象?
JOLT转换方案:将非结构化JSON映射为文本-链接对列表
需求说明
需要把输入中flow数组内的文本条目(如^First text),与末尾对象里对应的链接(c-n数组的->字段)一一对应,整理成flow.links结构化数组,每个元素包含message(原文本)和link(对应链接)。
直接可用的JOLT转换规范
[ { "operation": "shift", "spec": { "flow": { "0": "flow.links[0].message", "2": "flow.links[1].message", "4": { "c-0": { "0": { "->": "flow.links[0].link" } }, "c-1": { "0": { "->": "flow.links[1].link" } } } } } }, { "operation": "remove", "spec": { "flow": { "0": "", "1": "", "2": "", "3": "", "4": "" } } } ]
规范细节解释
- Shift操作(核心映射)
- 直接定位输入
flow数组的索引位置:把第1个文本(索引0)映射到links[0].message,第3个文本(索引2)映射到links[1].message - 定位末尾的链接存储对象(索引4),将
c-0下的链接映射到links[0].link,c-1下的链接映射到links[1].link
- 直接定位输入
- Remove操作(清理冗余)
- 移除原
flow数组里的旧元素,确保最终输出只保留整理后的links结构化数据
- 移除原
验证示例
输入JSON:
{ "flow": [ "^First text", { "*": ".^.c-0" }, "^Second text", { "*": ".^.c-1" }, { "c-0": [ { "->": "Link of first text" } ], "c-1": [ { "->": "Link of second text" } ] } ] }
执行转换后输出:
{ "flow": { "links": [ { "message": "^First text", "link": "Link of first text" }, { "message": "^Second text", "link": "Link of second text" } ] } }
扩展适配
如果后续新增文本-链接对(比如第3组对应c-2),只需在Shift规范中添加对应映射:
- 文本索引(对应数组位置)映射到
flow.links[2].message - 链接路径
flow[对应索引].c-2[0].->映射到flow.links[2].link
内容的提问来源于stack exchange,提问作者Lutz
相关产品推荐
相关产品推荐

