使用JOLT Parser无法获取预期输出,请求排查Spec错误原因
First, let's break down the error you're encountering:
Error running the Transform. JOLT Chainr 'operation' must implement Transform or ContextualTransform at index:0.
This error occurs because your JOLT spec is missing a required operation type. Every JOLT transform step needs to explicitly declare what kind of transformation it's performing (like shift, modify, or default). Your current spec doesn't include this key, so JOLT can't recognize it as a valid transform.
Correct JOLT Spec to Get Your Desired Output
To split the engines array into separate objects while retaining the parent ownerId and vehicleId, use a shift operation with this spec:
[ { "operation": "shift", "spec": { "*": { "engines": { "*": { "@2,ownerId": "[&1].ownerId", "@2,vehicleId": "[&1].vehicleId", "@": "[&1].engines" } } } } } ]
How This Spec Works:
"operation": "shift"tells JOLT we're rearranging data from the input structure to the output structure.- The outer
*targets the single object in your input array. "engines": { "*": { ... } }iterates over each element in theenginesarray.@2,ownerIdand@2,vehicleId: The@2means "go up 2 levels in the input hierarchy" (from the current engine object to the parent object holdingownerIdandvehicleId), then grabs those values."[&1].ownerId": The&1refers to the index of the current engine in theenginesarray, so each engine becomes a separate element in the output array.@copies the entire current engine object into theenginesfield of the new output object.
Testing the Spec
When you run this transform on your input:
[ { "ownerId": "XTV7I728", "vehicleId": "999", "engines": [ { "engineId": "1", "engineName": "Standard Engine", "engineHp": 300 }, { "engineId": "2", "engineName": "Custom Engine", "engineHp": 450 } ] } ]
You'll get exactly your desired output:
[{ "ownerId": "XTV7I728", "vehicleId": "999", "engines":{ "engineId": "1", "engineName": "Standard Engine", "engineHp": 300 } }, { "ownerId": "XTV7I728", "vehicleId": "999", "engines":{ "engineId": "2", "engineName": "Custom Engine", "engineHp": 450 } }]
内容的提问来源于stack exchange,提问作者whatsinthename

