Dynamic Jolt spec to handle when one nested array is present and and if nested array is not present
My jolt spec is working perfectly fine if we receive the nested array, but if they send a JSON without the nested array, its failing. Is there a way to make it work for both cases
Input 1 :
[
{
"payload": {
"header": {
"sender": "GRPSPM"
},
"article": {
"groupModelNumber": "LPZ48",
"groupArticleNumber": "IF5561",
"rmh": [
{
"rmhMarketingDivision": "1",
"rmhProductDivision": "1",
"rmhGender": "2",
"rmhCategory": "20",
"rmhProductType": "09",
"retailDepartment": "11",
"retailSubDepartment": "112",
"retailClass": "11220",
"retailSubClass": "1122009"
}
],
"sku": [
{
"skuChange": false,
"sourcingSizeScale": "T1",
"sourcingSizeCode2": "28",
"sourcingSizeCode3": "610",
"eanNumber": "4066765529183",
"upcNumber": "196472210778",
"feasibilityIndicator": true,
"activeIndicator": true
},
{
"skuChange": false,
"sourcingSizeScale": "T1",
"sourcingSizeCode2": "31",
"sourcingSizeCode3": "640",
"eanNumber": "4066765529169",
"upcNumber": "196472210761",
"feasibilityIndicator": true,
"activeIndicator": true
}
]
}
}
}
]
Input 2 :
[
{
"payload": {
"header": {
"sender": "GRPSPM"
},
"article": {
"groupModelNumber": "LPZ48",
"groupArticleNumber": "IF5561",
"rmh": [
{
"rmhMarketingDivision": "1",
"rmhProductDivision": "1",
"rmhGender": "2",
"rmhCategory": "20",
"rmhProductType": "09",
"retailDepartment": "11",
"retailSubDepartment": "112",
"retailClass": "11220",
"retailSubClass": "1122009"
}
]
}
}
}
]
Jolt Spec which i am currently using
[
{
"operation": "shift",
"spec": {
"@": "input",
"@(0)": "val0"
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"val0": "=toString",
"chr": ["=substring(@(1,val0),0,1)", "["]
}
},
{
"operation": "shift",
"spec": {
"chr": {
"{": {
"@(2,input)": "[]"
},
"*": {
"@(2,input)": ""
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"payload": {
"article": {
"groupModelNumber": "groupModelNumber",
"groupArticleNumber": "groupArticleNumber",
"sku": {
"*": {
"sourcingSizeCode3": "sourcingSizeCode3",
"feasibilityIndicator": "feasibilityIndicator",
"activeIndicator": "activeIndicator"
}
},
"rmh": {
"*": {
"retailClass": "retailClass",
"retailSubClass": "retailSubClass",
"retailDepartment": "retailDepartment",
"retailSubDepartment": "retailSubDepartment",
"rmhProductDivision": "rmhProductDivision",
"rmhCategory": "rmhCategory",
"rmhProductType": "rmhProductType"
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"sourcingSizeCode3": {
"*": {
"@(2,groupModelNumber)": "[#2].groupModelNumber",
"@": "[#2].&2",
"@(2,retailClass)": "[#2].retailClass",
"@(2,retailSubClass)": "[#2].retailSubClass",
"@(2,retailDepartment)": "[#2].retailDepartment",
"@(2,retailSubDepartment)": "[#2].retailSubDepartment",
"@(2,rmhProductDivision)": "[#2].rmhProductDivision",
"@(2,rmhCategory)": "[#2].rmhCategory",
"@(2,rmhProductType)": "[#2].rmhProductType"
}
},
"activeIndicator": {
"*": {
"@": "[#2].&2"
}
},
"feasibilityIndicator": {
"*": {
"@": "[#2].&2"
}
}
}
}
]
Output Expected for Input 1 :
[
{
"groupModelNumber": "LPZ48",
"sourcingSizeCode3": "610",
"retailClass": "11220",
"retailSubClass": "1122009",
"retailDepartment": "11",
"retailSubDepartment": "112",
"rmhProductDivision": "1",
"rmhCategory": "20",
"rmhProductType": "09",
"activeIndicator": true,
"feasibilityIndicator": true
},
{
"groupModelNumber": "LPZ48",
"sourcingSizeCode3": "640",
"retailClass": "11220",
"retailSubClass": "1122009",
"retailDepartment": "11",
"retailSubDepartment": "112",
"rmhProductDivision": "1",
"rmhCategory": "20",
"rmhProductType": "09",
"activeIndicator": true,
"feasibilityIndicator": true
}
]
Output Expected for Input 2 :
{
"groupModelNumber" : "LPZ48",
"groupArticleNumber" : "IF5561",
"retailClass" : "11220",
"retailSubClass" : "1122009",
"retailDepartment" : "11",
"retailSubDepartment" : "112",
"rmhProductDivision" : "1",
"rmhCategory" : "20",
"rmhProductType" : "09"
}
You can combine the cases while shortening the whole transformation as
[
{
"operation": "shift",
"spec": {
"*": {
"payload": {
"article": {
"sku|rmh": {
"*": {
"@(2,groupModelNumber)": "[#2].groupModelNumber",
"@(2,groupArticleNumber)": "[#2].groupArticleNumber",
"sourcingSizeCode3|feasibilityIndicator|activeIndicator": "[#2].&",
"@(2,rmh[0].retailClass)": "[#2].retailClass",
"@(2,rmh[0].retailSubClass)": "[#2].retailSubClass",
"@(2,rmh[0].retailDepartment)": "[#2].retailDepartment",
"@(2,rmh[0].retailSubDepartment)": "[#2].retailSubDepartment",
"@(2,rmh[0].rmhProductDivision)": "[#2].rmhProductDivision",
"@(2,rmh[0].rmhCategory)": "[#2].rmhCategory",
"@(2,rmh[0].rmhProductType)": "[#2].rmhProductType"
}
}
}
}
}
}
},
{ // only pick one component from the repeating components for the innermost arrays (if they're array)
"operation": "cardinality",
"spec": {
"*": {
"*": "ONE"
}
}
},
{ // remove array wrapper if the current array contains single object
"operation": "shift",
"spec": {
"*": {
"@": ""
}
}
}
]