Search code examples
jsonapache-nifijolt

Need help in flattening a complex Nested JSON using Jolt Transform


I am facing a problem, transforming a very complex nested JSON using jolt transformation. Input and output detail is given below.

Input

{
  "guid": "String 1",
  "WebHookId": 1234567890,
  "siteLocation": "offshore",
  "ReferenceCountry": [
    {
      "RefCountry": "DE",
      "Country": "NL"
    }
  ],
  "WTGLevel": [
    {
      "SCID": "5gmqATexwYw",
      "ReferenceCountry": "DE",
      "Material": "WTG_01",
      "Description": "CS-048948A-#01",
      "Quantity": 1,
      "UnitOfMeasure": "EA",
      "WTGType": "V136-4.0MW",
      "RotorDiameter": "136 m",
      "MarkRelease": "3F",
      "NominalEffect": 4200,
      "HubHeight": 112,
      "component": [
        {
          "Material": "NACELLE_01",
          "Description": "Nacelle MSI",
          "Quantity": 1,
          "UnitOfMeasure": "EA",
          "CapacityType": "N_V136_4.0MW_TR_3F",
          "MaterialGroup": 325,
          "DerivedMaterial": 29227496,
          "MaterialIndicator": "PLAN_LO",
          "PriceBaseID": "P/NAC:P_3MW/MK_3F/R_136M/FRQ_50HZ/LOC_04"
        },
        {
          "Material": "SP_AVI_LAMP_01",
          "Description": "Site Parts Aviation Lamp MSI",
          "Quantity": 1,
          "UnitOfMeasure": "EA",
          "CapacityType": "NAC_OPT",
          "MaterialGroup": 5,
          "DerivedMaterial": 29197892,
          "MaterialIndicator": "VAR",
          "PriceBaseID": "P/AV_LA:AVWTG_3MW/AV_LA_18/AV_Q_1"
        }
      ]
    },
    {
      "SCID": "zyUL6lemDdM",
      "ReferenceCountry": "DE",
      "Material": "WTG_01",
      "Description": "CS-042692A-#01",
      "Quantity": 1,
      "UnitOfMeasure": "EA",
      "WTGType": "V136-4.0MW",
      "RotorDiameter": "136 m",
      "MarkRelease": "3E",
      "NominalEffect": 4.2,
      "HubHeight": 112,
      "component": [
        {
          "Material": "BLADE_01",
          "Description": "Blade MSI",
          "Quantity": 3,
          "UnitOfMeasure": "EA",
          "CapacityType": "BLA_67M_STR",
          "MaterialGroup": 40,
          "DerivedMaterial": 29156738,
          "MaterialIndicator": "VAR",
          "PriceBaseID": "P/BLA:P_3MW/MK_3E/R_136M/DEICE_N/BLATY_00"
        },
        {
          "Material": "DRIVE_TRAIN_01",
          "Description": "Drive Train MSI",
          "Quantity": 1,
          "UnitOfMeasure": "EA",
          "CapacityType": "DT_V136_4.0ME_3E",
          "MaterialGroup": 540,
          "DerivedMaterial": 29219265,
          "MaterialIndicator": "VAR",
          "PriceBaseID": "P/DT:P_3MW/MK_3E/R_136M/LOC_01"
        }
      ]
    }
  ]
}

Output Expected

[
  {
    "guid": "String 1",
    "WebHookId": 1234567890,
    "siteLocation": "offshore",
    "RefCountry": "DE",
    "Country": "NL",
    "SCID": "5gmqATexwYw",
    "ReferenceCountry": "DE",
    "Material": "WTG_01",
    "Description": "CS-048948A-#01",
    "Quantity": 1,
    "UnitOfMeasure": "EA",
    "WTGType": "V136-4.0MW",
    "RotorDiameter": "136 m",
    "MarkRelease": "3F",
    "NominalEffect": 4200,
    "HubHeight": 112,
    "Cmpt_Material": "NACELLE_01",
    "Cmpt_Quantity": 1,
    "Cmpt_UnitOfMeasure": "EA",
    "CapacityType": "N_V136_4.0MW_TR_3F"
  },
  {
    "guid": "String 1",
    "WebHookId": 1234567890,
    "siteLocation": "offshore",
    "RefCountry": "DE",
    "Country": "NL",
    "SCID": "5gmqATexwYw",
    "ReferenceCountry": "DE",
    "Material": "WTG_01",
    "Description": "CS-048948A-#01",
    "Quantity": 1,
    "UnitOfMeasure": "EA",
    "WTGType": "V136-4.0MW",
    "RotorDiameter": "136 m",
    "MarkRelease": "3E",
    "NominalEffect": 4.2,
    "HubHeight": 112,
    "Cmpt_Material": "SP_AVI_LAMP_01",
    "Cmpt_Quantity": 1,
    "Cmpt_UnitOfMeasure": "EA",
    "CapacityType": "NAC_OPT"
  },
  {
    "guid": "String 1",
    "WebHookId": 1234567890,
    "siteLocation": "offshore",
    "RefCountry": "DE",
    "Country": "NL",
    "SCID": "zyUL6lemDdM",
    "ReferenceCountry": "DE",
    "Material": "BLADE_01",
    "Description": "CS-042692A-#01",
    "Quantity": 1,
    "UnitOfMeasure": "EA",
    "WTGType": "V136-4.0MW",
    "RotorDiameter": "136 m",
    "MarkRelease": "3E",
    "NominalEffect": 4.2,
    "HubHeight": 112,
    "Cmpt_Material": "BLADE_01",
    "Cmpt_Quantity": 1,
    "Cmpt_UnitOfMeasure": "EA",
    "CapacityType": "NAC_OPT"
  },
  {
    "guid": "String 1",
    "WebHookId": 1234567890,
    "siteLocation": "offshore",
    "RefCountry": "DE",
    "Country": "NL",
    "SCID": "5gmqATexwYw",
    "ReferenceCountry": "DE",
    "Material": "BLADE_01",
    "Description": "CS-042692A-#01",
    "Quantity": 1,
    "UnitOfMeasure": "EA",
    "WTGType": "V136-4.0MW",
    "RotorDiameter": "136 m",
    "MarkRelease": "3E",
    "NominalEffect": 4.2,
    "HubHeight": 112,
    "Cmpt_Material": "DRIVE_TRAIN_01",
    "Cmpt_Quantity": 1,
    "Cmpt_UnitOfMeasure": "EA",
    "CapacityType": "NAC_OPT"
  }
]

Component Array values are prefixed with the value Cmpt_*. There might be 2 or many SCID's and in under each SCID's there might be multiple components.

Pls help me with this one.


Solution

  • You can use the following shift transformation specs

    [
      {
        "operation": "shift",
        "spec": {
          "WTGLevel": {
            "*": {
              "component": {
                "*": { // partition the objects by indexes from two different levels
                  "@4,guid": "&3_&1.guid",
                  "@4,WebHookId": "&3_&1.WebHookId",
                  "@4,siteLocation": "&3_&1.siteLocation",
                  "@4,ReferenceCountry[0].RefCountry": "&3_&1.RefCountry",
                  "@4,ReferenceCountry[0].Country": "&3_&1.Country",
                  "@2,SCID": "&3_&1.SCID",
                  "@2,ReferenceCountry": "&3_&1.ReferenceCountry",
                  "@2,Material": "&3_&1.Material",
                  "@2,Description": "&3_&1.Description",
                  "@2,HubHeight": "&3_&1.HubHeight",
                  "@2,Quantity": "&3_&1.Quantity",
                  "@2,UnitOfMeasure": "&3_&1.UnitOfMeasure",
                  "@2,WTGType": "&3_&1.WTGType",
                  "@2,RotorDiameter": "&3_&1.RotorDiameter",
                  "@2,MarkRelease": "&3_&1.MarkRelease",
                  "@2,NominalEffect": "&3_&1.NominalEffect",
                  "Material|Quantity|UnitOfMeasure": "&3_&1.Cmpt_&",
                  "CapacityType": "&3_&1.&"
                }
              }
            }
          }
        }
      },
      { // add an array wrapper at the topmost level while getting rid of newly formed object labels
        "operation": "shift",
        "spec": {
          "*": "[]"
        }
      }
    ]