处理对象数组和单个对象的动态Jolt规范



动态Jolt规范,用于处理对象数组和单个对象。

如果我们接收到一组对象,我的震动规范工作得很好,但如果他们发送一个没有数组的对象,它就会失败。有没有一种方法可以处理震动规范中的对象和单个对象的数组?

你好,

这是我输入的JSON

[
{
"PC9Code": "94520-0000",
"ProductDescription": "94520 STANDARD SHORT STEEL BLACK ADV SHO",
"MaterialType": "Finished Goods",
"ProductType": "REGULAR",
"Brand": "LEVIS",
"ProductCategory": "BOTTOMS",
"ConsumerGroup": "MEN",
"Gender": "MALE",
"CapsuleName": "0",
"FFCCode": "X3209",
"FFCName": "STEEL BLACK ADV SHORT",
"StyleCode": "94520",
"StyleName": "94520 STANDARD SHORT",
"ProductLine": "Levi's Mainline",
"ProductSubCategory": "SHORTS",
"ProductClass": "SHORTS",
"ProductSubClass": "MID LENGTH SHORTS",
"CarryOver": "N",
"ProductPricePositioningDesc": "TIER 3",
"AgilityIndicator": "N",
"OriginalBFF": "0",
"ProductLifeCycle": "SEASONAL",
"ColorCode": "X3209",
"ColorName": "STEEL BLACK ADV SHOR",
"EarlyDelivery": "NO",
"FirstOnFloorMonth": "1",
"GlobalPlanningView": "0",
"UOM": "EA",
"PC13": [
{
"SKU": "0050100003030",
"DIM1": "30",
"DIM2": "30"
},
{
"SKU": "0050800003080",
"DIM1": "30",
"DIM2": "80"
}
],
"OperationType": "UPDATE",
"TimeStamp": "6/2/2022  4:52:17 PM"
}
]

我的Jolt Spec

[
{
"operation": "shift",
"spec": {
"*": {
"PC13": {
"*": {
"@": "&[&3]",
"@(2,AgilityIndicator)": "&[&3].AgilityIndicator",
"@(2,Brand)": "&[&3].Brand",
"@(2,CapsuleName)": "&[&3].CapsuleName",
"@(2,CarryOver)": "&[&3].CarryOver",
"@(2,ColorCode)": "&[&3].ColorCode",
"@(2,ColorName)": "&[&3].ColorName",
"@(2,ConsumerGroup)": "&[&3].ConsumerGroup",
"@(2,EarlyDelivery)": "&[&3].EarlyDelivery",
"@(2,FFCCode)": "&[&3].FFCCode",
"@(2,FFCName)": "&[&3].FFCName",
"@(2,FirstOnFloorMonth)": "&[&3].FirstOnFloorMonth",
"@(2,Gender)": "&[&3].Gender",
"@(2,GlobalPlanningView)": "&[&3].GlobalPlanningView",
"@(2,MaterialType)": "&[&3].MaterialType",
"@(2,OperationType)": "&[&3].OperationType",
"@(2,OriginalBFF)": "&[&3].OriginalBFF",
"@(2,PC9Code)": "&[&3].PC9Code",
"@(2,ProductCategory)": "&[&3].ProductCategory",
"@(2,ProductClass)": "&[&3].ProductClass",
"@(2,ProductDescription)": "&[&3].ProductDescription",
"@(2,ProductLifeCycle)": "&[&3].ProductLifeCycle",
"@(2,ProductLine)": "&[&3].ProductLine",
"@(2,ProductPricePositioningDesc)": "&[&3].ProductPricePositioningDesc",
"@(2,ProductSubCategory)": "&[&3].ProductSubCategory",
"@(2,ProductSubClass)": "&[&3].ProductSubClass",
"@(2,ProductType)": "&[&3].ProductType",
"@(2,StyleCode)": "&[&3].StyleCode",
"@(2,StyleName)": "&[&3].StyleName",
"@(2,TimeStamp)": "&[&3].TimeStamp",
"@(2,UOM)": "&[&3].UOM"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": ""
}
}
}
]

但如果他们像单个物体一样发送,我的震动规范就失败了

{
"PC9Code": "94520-0000",
"ProductDescription": "94520 STANDARD SHORT STEEL BLACK ADV SHO",
"MaterialType": "Finished Goods",
"ProductType": "REGULAR",
"Brand": "LEVIS",
"ProductCategory": "BOTTOMS",
"ConsumerGroup": "MEN",
"Gender": "MALE",
"CapsuleName": "0",
"FFCCode": "X3209",
"FFCName": "STEEL BLACK ADV SHORT",
"StyleCode": "94520",
"StyleName": "94520 STANDARD SHORT",
"ProductLine": "Levi's Mainline",
"ProductSubCategory": "SHORTS",
"ProductClass": "SHORTS",
"ProductSubClass": "MID LENGTH SHORTS",
"CarryOver": "N",
"ProductPricePositioningDesc": "TIER 3",
"AgilityIndicator": "N",
"OriginalBFF": "0",
"ProductLifeCycle": "SEASONAL",
"ColorCode": "X3209",
"ColorName": "STEEL BLACK ADV SHOR",
"EarlyDelivery": "NO",
"FirstOnFloorMonth": "1",
"GlobalPlanningView": "0",
"UOM": "EA",
"PC13": [
{
"SKU": "0050100003030",
"DIM1": "30",
"DIM2": "30"
},
{
"SKU": "0050800003080",
"DIM1": "30",
"DIM2": "80"
}
],
"OperationType": "UPDATE",
"TimeStamp": "6/2/2022  4:52:17 PM"
}

您可以通过在modify转换规范中使用子字符串函数来字符串化JSON,以检查第一个字符是否以{开头(例如object(,同时保留初始JSON值。如果它是一个对象,那么通过在移位转换中使用条件(如(将其括在方括号内

[
{
"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)": ""
}
}
}
},
// your current spec starts here
{
"operation": "shift",
"spec": {
"*": {
"PC13": {
"*": {
"@": "&[&3]",
"@(2,AgilityIndicator)": "&[&3].AgilityIndicator",
"@(2,Brand)": "&[&3].Brand",
"@(2,CapsuleName)": "&[&3].CapsuleName",
"@(2,CarryOver)": "&[&3].CarryOver",
"@(2,ColorCode)": "&[&3].ColorCode",
"@(2,ColorName)": "&[&3].ColorName",
"@(2,ConsumerGroup)": "&[&3].ConsumerGroup",
"@(2,EarlyDelivery)": "&[&3].EarlyDelivery",
"@(2,FFCCode)": "&[&3].FFCCode",
"@(2,FFCName)": "&[&3].FFCName",
"@(2,FirstOnFloorMonth)": "&[&3].FirstOnFloorMonth",
"@(2,Gender)": "&[&3].Gender",
"@(2,GlobalPlanningView)": "&[&3].GlobalPlanningView",
"@(2,MaterialType)": "&[&3].MaterialType",
"@(2,OperationType)": "&[&3].OperationType",
"@(2,OriginalBFF)": "&[&3].OriginalBFF",
"@(2,PC9Code)": "&[&3].PC9Code",
"@(2,ProductCategory)": "&[&3].ProductCategory",
"@(2,ProductClass)": "&[&3].ProductClass",
"@(2,ProductDescription)": "&[&3].ProductDescription",
"@(2,ProductLifeCycle)": "&[&3].ProductLifeCycle",
"@(2,ProductLine)": "&[&3].ProductLine",
"@(2,ProductPricePositioningDesc)": "&[&3].ProductPricePositioningDesc",
"@(2,ProductSubCategory)": "&[&3].ProductSubCategory",
"@(2,ProductSubClass)": "&[&3].ProductSubClass",
"@(2,ProductType)": "&[&3].ProductType",
"@(2,StyleCode)": "&[&3].StyleCode",
"@(2,StyleName)": "&[&3].StyleName",
"@(2,TimeStamp)": "&[&3].TimeStamp",
"@(2,UOM)": "&[&3].UOM"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": ""
}
}
}
]

最新更新