JOLT新手求助:如何将类表格JSON转换为规范JSON?
JOLT转换类表格JSON解决方案
输入示例
{ "Sections": [ { "Title": "用户信息", "Rows": [ { "RowType": "Header", "Cells": [{"Value": "字段"}, {"Value": "内容"}] }, { "RowType": "Data", "Cells": [{"Value": "姓名"}, {"Value": "张三"}] }, { "RowType": "Data", "Cells": [{"Value": "年龄"}, {"Value": "30"}] } ] }, { "Title": "订单信息", "Rows": [ { "RowType": "Header", "Cells": [{"Value": "字段"}, {"Value": "内容"}] }, { "RowType": "Data", "Cells": [{"Value": "订单号"}, {"Value": "OD20240501"}] }, { "RowType": "Data", "Cells": [{"Value": "金额"}, {"Value": "199"}] } ] } ] }
JOLT转换规范
[ { "operation": "shift", "spec": { "Sections": { "*": { "Rows": { "*": { "RowType": { "Data": { "@(2,Cells[0].Value)": "@(3,Title).@(2,Cells[1].Value)" } } } } } } } }, { "operation": "shift", "spec": { "*": { "$": "&2.&" } } }, { "operation": "cardinality", "spec": { "*": { "*": "ONE" } } } ]
输出结果
{ "用户信息": { "姓名": "张三", "年龄": "30" }, "订单信息": { "订单号": "OD20240501", "金额": "199" } }
关键步骤说明
- 第一步shift:遍历所有Section,过滤掉
RowType为Header的行,同时把数据行的Cells[0].Value暂存为值,Cells[1].Value暂存为键,关联到对应Section的Title下 - 第二步shift:反转暂存的键值对,把
Cells[0].Value设为最终的键,Cells[1].Value设为对应的值 - cardinality操作:确保每个字段只保留唯一值,避免重复行导致的数组结构
内容的提问来源于stack exchange,提问作者Clive
相关产品推荐
相关产品推荐

