Jolt Shift转换数组输出出现Null,求修正方案
Hey there! I see the annoying null value popping up in your second object's LOCATIONS array—let's get this sorted out right away.
The Root Cause
The issue comes from how you're indexing the LOCATIONS array in your shift spec. Right now you're using [&1] for the LOCATIONS index:
"BUILDING": "[&1].LOCATIONS.[&1].Building",
That inner &1 refers to the index of the parent COMPINFO array element. For the first COMPINFO entry (index 0), this maps to LOCATIONS[0] which works perfectly. But for the second entry (index 1), it tries to map to LOCATIONS[1]—leaving the LOCATIONS[0] slot empty, which gets filled with null.
The Simple Fix
Since each COMPINFO entry should only have one location object in its LOCATIONS array, replace the inner [&1] with a fixed [0] index. Here's the corrected spec:
[ { "operation": "shift", "spec": { "data": { "COMPINFO": { "*": { "COMPID": "[&1].COMPID", "ORGID": "[&1].ORGID", "COMP_DESC": "[&1].DESCRIPTION", "BUILDING": "[&1].LOCATIONS.[0].Building", "FLR_LEVEL": "[&1].LOCATIONS.[0].Floor_Level", "ROOM_SPCE": "[&1].LOCATIONS.[0].ROOM_SPCE", "SITEID": "[&1].SITEID", "ASSETTYPE": "[&1].ASSETTYPE" } } } } } ]
Verified Output
With this adjusted spec, your output will match exactly what you expected:
[ { "COMPID": "C2014", "ORGID": "", "DESCRIPTION": "Cherokee High School", "LOCATIONS": [ { "Building": "Main", "Floor_Level": "Ground", "ROOM_SPCE": "100" } ], "SITEID": "BWDEMO", "ASSETTYPE": "MANUFACTURING" }, { "COMPID": "9001B", "ORGID": "", "DESCRIPTION": "Sludge Pump", "LOCATIONS": [ { "Building": "Main", "Floor_Level": "Production", "ROOM_SPCE": "100" } ], "SITEID": "BWDEMO", "ASSETTYPE": "FACILITY" } ]
No more unwanted nulls—each LOCATIONS array now holds exactly one location object as intended!
内容的提问来源于stack exchange,提问作者Pratik Jaiswal

