Home > Back-end >  Dynamic Jolt spec to handle both the array of object and single object
Dynamic Jolt spec to handle both the array of object and single object

Time:10-19

Dynamic Jolt spec to handle both the array of object and single object.

My jolt spec is working perfectly fine if we receive the array of objects, but if they send a single object without the array, its failing. Is there a way to the array of object and single object in jolt spec?

Hi,

Here is my input 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"
  }
]

My 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": {
      "*": {
        "*": ""
      }
    }
  }
]

But my jolt spec is failing if they are sending like a single object

{
  "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"
}

CodePudding user response:

You can stringify the JSON to check whether the first character starts with {(eg.object) or not by using a substring function within a modify transformation spec, while keeping the initial JSON value aside. If it's an object, then wrap up within square brackets through use of a conditional within a shift transformation such as

[
  {
    "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": {
      "*": {
        "*": ""
      }
    }
  }
]
  • Related