求助:使用Jolt转换扁平化复杂嵌套货运JSON
修正Jolt Spec实现货运JSON扁平化输出
输入JSON示例
{ "shipment": { "id": "SHP-001", "carrier": "XYZ Logistics", "consignments": [ { "id": "CON-001", "shipper": "ABC Corp", "packages": [ { "id": "PKG-001", "weight": 15.5, "dimensions": { "length": 30, "width": 20, "height": 15 }, "trackingNumber": "TRK-12345" }, { "id": "PKG-002", "weight": 8.2, "dimensions": { "length": 25, "width": 15, "height": 10 }, "trackingNumber": "TRK-67890" } ] } ] } }
期望输出数组
[ { "shipmentId": "SHP-001", "carrier": "XYZ Logistics", "consignmentId": "CON-001", "shipper": "ABC Corp", "packageId": "PKG-001", "weight": 15.5, "length": 30, "width": 20, "height": 15, "trackingNumber": "TRK-12345" }, { "shipmentId": "SHP-001", "carrier": "XYZ Logistics", "consignmentId": "CON-001", "shipper": "ABC Corp", "packageId": "PKG-002", "weight": 8.2, "length": 25, "width": 15, "height": 10, "trackingNumber": "TRK-67890" } ]
原失败Jolt Spec
[ { "operation": "shift", "spec": { "shipment": { "id": "shipmentId", "carrier": "carrier", "consignments": { "*": { "id": "consignmentId", "shipper": "shipper", "packages": { "*": { "id": "packageId", "weight": "weight", "dimensions": { "length": "length", "width": "width", "height": "height" }, "trackingNumber": "trackingNumber" } } } } } } } ]
修正后的Jolt Spec
[ { "operation": "shift", "spec": { "shipment": { "id": "[&1].shipmentId", "carrier": "[&1].carrier", "consignments": { "*": { "id": "[&2].[&1].consignmentId", "shipper": "[&2].[&1].shipper", "packages": { "*": { "id": "[&3].[&2].[&1].packageId", "weight": "[&3].[&2].[&1].weight", "dimensions": { "length": "[&4].[&3].[&2].[&1].length", "width": "[&4].[&3].[&2].[&1].width", "height": "[&4].[&3].[&2].[&1].height" }, "trackingNumber": "[&3].[&2].[&1].trackingNumber" } } } } } } }, { "operation": "flatten", "spec": { "*": { "*": { "*": "[]" } } } } ]
关键修正点
- 原Spec未使用数组索引占位符(
&n)关联层级关系,导致嵌套数组的属性无法正确映射到同一对象。修正后的shift操作通过[&n]定位不同层级的数组索引,确保每个包裹属性与所属运单、托运单一一绑定。 - 新增
flatten操作将嵌套的三维数组结构直接展开为一维数组,完全匹配预期的扁平化输出格式。
内容的提问来源于stack exchange,提问作者Thiyagaraj Narayanan
相关产品推荐
相关产品推荐

