處理物件陣列和單個物件的動態 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"
}
]
我的顛簸規格
[
{
"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": {
"*": {
"*": ""
}
}
}
]
但是,如果它們像單個物件一樣發送,我的 jolt 規范將失敗
{
"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"
}
uj5u.com熱心網友回復:
您可以通過在修改轉換規范中使用子字串函式對 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": {
"*": {
"*": ""
}
}
}
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/518547.html
標籤:json阿帕奇尼菲颠簸
上一篇:是否可以在有條件的情況下將json物件轉換為JOLT中的陣列?
下一篇:如何使用變數C#訪問我的嵌套資料
