请求提供Jolt转换规范:展开JSON嵌套数组结构
Jolt转换实现方案
输入JSON
[ { "doc_GUID": "ae5deb48-9807-11ef-aaeb-ec2a723f4399", "items": [ { "item_id": "00-00002475", "type_of_goods": "RR" }, { "item_id": "00-00001243", "type_of_goods": "TT" }, { "item_id": "00-00002997", "type_of_goods": "AA" } ] }, { "doc_GUID": "e83f9131-a034-11ef-aaeb-ec2a723f4399", "items": [ { "item_id": "00-00002828", "type_of_goods": "YY" }, { "item_id": "00-000029971", "type_of_goods": "EE" } ] } ]
目标输出JSON
[ { "doc_GUID": "ae5deb48-9807-11ef-aaeb-ec2a723f4399", "item_id": "00-00002475", "type_of_goods": "RR" }, { "doc_GUID": "ae5deb48-9807-11ef-aaeb-ec2a723f4399", "item_id": "00-00001243", "type_of_goods": "TT" }, { "doc_GUID": "ae5deb48-9807-11ef-aaeb-ec2a723f4399", "item_id": "00-00002997", "type_of_goods": "AA" }, { "doc_GUID": "e83f9131-a034-11ef-aaeb-ec2a723f4399", "item_id": "00-00002828", "type_of_goods": "YY" }, { "doc_GUID": "e83f9131-a034-11ef-aaeb-ec2a723f4399", "item_id": "00-000029971", "type_of_goods": "EE" } ]
Jolt转换规范
[ { "operation": "shift", "spec": { "*": { "items": { "*": { "@(2,doc_GUID)": "[#2].doc_GUID", "item_id": "[#2].item_id", "type_of_goods": "[#2].type_of_goods" } } } } } ]
规范说明
- shift操作:完成结构扁平化与字段映射,把嵌套的items数组展开为独立对象,同时继承父级的
doc_GUID。 - 外层
*:匹配输入数组中的每一个文档对象。 items:定位到每个文档下的items数组节点。- 内层
*:遍历items数组中的每一个item元素。 @(2,doc_GUID):通过相对路径向上回溯2层(从当前item节点到文档对象层级),获取对应的doc_GUID值。[#2]:为每个item生成一个独立的数组元素,保证输出为扁平化数组结构。
内容的提问来源于stack exchange,提问作者Alan M
相关产品推荐
相关产品推荐

