I had asked a question previously: JOLT Transform : Extract and combine fields from multiple JSON arrays to single array output
This is a bit different question, where the arrays can actually consist null values.
Example 1:
{
"questionResponses": [
{
"responses": null
}
],
"answers": [
{
"questionId": "1",
"answerId": "answer 1"
},
{
"questionId": "1",
"answerId": "answer 2"
}
],
"totalAttachments": 0
}
Undesired Output:
null
Expected Output:
[
{
"response": null,
"answerId": "answer 1"
},
{
"response": null,
"answerId": "answer 2"
}
]
Example 2:
{
"questionResponses": [
{
"responses": [
{
"responseId": "1",
"response": "response 1"
},
{
"responseId": "2",
"response": "response 2"
},
{
"responseId": "3",
"response": "response 3"
}
]
}
],
"answers": null,
"totalAttachments": 0
}
Undesired Output:
null
Expected Output:
[
{
"response": "response 1",
"answerId": null
},
{
"response": "response 2",
"answerId": null
},
{
"response": "response 3",
"answerId": null
}
]
JOLT Spec:
[
{
"operation": "shift",
"spec": {
"questionR*": {
"*": {
"responses": {
"*": {
"@(4,answers)": "@(1,response)"
}
}
}
}
}
},
{
// Determine two independent arrays both with size of 6
"operation": "shift",
"spec": {
"*": {
"*": {
"$1": "response",
"a*": "&"
}
}
}
},
{
// expand those arrays as objects which contain desired key-value pairs
"operation": "shift",
"spec": {
"*": {
"*": "&.&1"
}
}
},
{
// get rid of object labels
"operation": "shift",
"spec": {
"*": ""
}
}
]
The JOLT Spec works as expected when both of the arrays consists at least single value. But fails when either of them is null.

