bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.56k stars 329 forks source link

duplicate array elements in jolt transformation #1141

Open duke-nuke opened 2 years ago

duke-nuke commented 2 years ago

Hi folks

i am trying to transform a source json to destination json and finding it difficult to transform it correctly not sure how to copy/duplicated parent array elements into nested array.

Source Json

{ "id": "64843765e09f42afa1a9094249325c49", "type": "blob", "Id": "15042", "eId": "t0025443mhql", "Uuid": "94014349-0a7e-4c65-8955-81a478c4a37d", "sysId": "QA7CLNT400", "doc": { "lineItem": [ { "allocation": [ { "led": { "amountShipping": 0, "accountCode": "24140", "amountNet": 40, "amountGross": 40 }, "Custom5Value": "Cor", "Custom6Value": "Admin", "Custom7Value": "System", "Custom9Value": null, "Custom8Value": "test", "Custom11Value": null, "Custom10Value": null, "Custom12Value": null }, { "led": { "amountShipping": 0, "accountCode": "24140", "amountNet": 60, "amountGross": 60 }, "Custom5Value": null, "Custom6Value": null, "Custom7Value": "System", "Custom9Value": "RC", "Custom8Value": "test", "Custom11Value": null, "Custom10Value": "Hub", "Custom12Value": null } ], "tax": [ { "amountTax": 0, "taxCode": "P3", "taxField": "VAT1" } ], "expenseTypeName": "Resources", "poLineNumber": null, "externalLineItemId": null, "receiptNumbers": [], "ItemCustom5Value": null, "ItemCustom6Value": null, "ItemCustom7Value": null }, { "allocation": [ { "led": { "amountShipping": 0, "accountCode": "11958", "amountNet": 75, "amountGross": 82.5 }, "Custom5Value": null, "Custom6Value": null, "Custom7Value": "tem", "Custom9Value": "res", "Custom8Value": "lia", "Custom11Value": "rse", "Custom10Value": "port", "Custom12Value": null }, { "led": { "amountShipping": 0, "accountCode": "11958", "amountNet": 175, "amountGross": 192.5 }, "Custom5Value": null, "Custom6Value": null, "Custom7Value": "System", "Custom9Value": null, "Custom8Value": "aa", "Custom11Value": "rse", "Custom10Value": "port", "Custom12Value": null } ], "tax": [ { "amountTax": 25, "taxCode": "P1", "taxField": "VAT1" } ], "expenseTypeName": "Costs", "poLineNumber": null, "LineItemId": null, "receiptNumbers": [], "ItemCustom5Value": null, "ItemCustom6Value": null, "ItemCustom7Value": null, "ItemCustom9Value": null } ] }, "Blob": null, "Status": "READY", "urls": [] }

output

{ "ItemSet" : [ { "Account" : [ "24140", "24140" ], "ClaimedAmount" : [ 40, 40, 60, 60 ], "GrossAmount" : [ 40, 60 ], "CostObjTyp" : [ "Cor", null ], "SalesOrder" : [ "Admin", null ], "SalesOrderItem" : "RC", "CostCenter" : "Hub", "allocationCustom1Code" : null, "allocationCustom11Code" : null, "ExpenseType" : "Resources" }, { "Account" : [ "11958", "11958" ], "ClaimedAmount" : [ 75, 75, 175, 175 ], "GrossAmount" : [ 82.5, 192.5 ], "CostObjTyp" : null, "SalesOrder" : null, "SalesOrderItem" : [ "res", null ], "CostCenter" : [ "port", "port" ], "allocationCustom1Code" : [ "rse", "rse" ], "allocationCustom11Code" : null, "ExpenseType" : "Costs" } ], "ItemSet" : [ { "NetAmount" : [ 40, 60 ] }, { "NetAmount" : [ 75, 175 ] } ] }

Jolt Specs

[{ "operation": "shift", "spec": { "doc": { "lineItem": { "": { "allocation": { "": { "led": { "accountCode": "ItemSet[&4].Account", "amountNet": ["ItemSet[&4].ClaimedAmount", "ItemSet[&4].NetAmount", "ItemSet[&4].ClaimedAmount"], "amountGross": "ItemSet[&4].GrossAmount" }, "Custom5Value": "ItemSet[&3].CostObjTyp", "Custom6Value": "ItemSet[&3].SalesOrder", "Custom9Value": "ItemSet[&3].SalesOrderItem", "Custom10Value": "ItemSet[&3].CostCenter", "Custom11Value": "ItemSet[&3].allocationCustom1Code", "Custom12Value": "ItemSet[&3].allocationCustom11Code" } }, "expenseTypeName": "ItemSet[&1].ExpenseType", "ItemDescription": "ItemSet[&1].ItemDesc", "ItemCustom15Code": "ItemSet[&1].CompCode", "ItemCustom17Code": "ItemSet[&1].PersonnelNo" } } } } }

]

Expected Output

{ "ItemSet": [ { "Account": "24140", "ClaimedAmount": 40, "GrossAmount": 40, "CostObjTyp": "Cor", "SalesOrder": "Admin", "SalesOrderItem": "RC", "CostCenter": "Hub", "allocationCustom1Code": null, "allocationCustom11Code": null, "ExpenseType": "Resources" }, { "Account": "24140", "ClaimedAmount": 40, "GrossAmount": 60, "CostObjTyp": null, "SalesOrder": null, "SalesOrderItem": "RC", "CostCenter": "Hub", "allocationCustom1Code": null, "allocationCustom11Code": null, "ExpenseType": "Resources" }, { "Account": "11958", "ClaimedAmount": 75, "GrossAmount": 82.5, "CostObjTyp": null, "SalesOrder": null, "SalesOrderItem": "res", "CostCenter": "port", "allocationCustom1Code": "rse", "allocationCustom11Code": null, "ExpenseType": "Costs" }, { "Account": "11958", "ClaimedAmount": 75, "GrossAmount": 192.5, "CostObjTyp": null, "SalesOrder": null, "SalesOrderItem": null, "CostCenter": "port", "allocationCustom1Code": "rse", "allocationCustom11Code": null, "ExpenseType": "Costs" } ] }

dinesh-rajaveeran commented 2 years ago

I'll give your some direction to try:


[{
    "operation": "shift",
    "spec": {
      "doc": {
        "lineItem": {
          "*": {
            "allocation": {
              "*": {
                "led": {
                  "accountCode": "ItemSet.Account",
                  "amountNet": "ItemSet.ClaimedAmount",
                  "amountGross": "ItemSet.GrossAmount"
                },
                "Custom5Value": "ItemSet.CostObjTyp",
                "Custom6Value": "ItemSet.SalesOrder",
                "Custom9Value": "ItemSet.SalesOrderItem",
                "Custom10Value": "ItemSet.CostCenter",
                "Custom11Value": "ItemSet.allocationCustom1Code",
                "Custom12Value": "ItemSet.allocationCustom11Code"
              }
            },
            "expenseTypeName": "ItemSet.ExpenseType",
            "ItemDescription": "ItemSet.ItemDesc",
            "ItemCustom15Code": "ItemSet.CompCode",
            "ItemCustom17Code": "ItemSet.PersonnelNo"
          }
        }
      }
    }
},
  {
    "operation": "shift",
    "spec": {
      "ItemSet": {
        "Account": {
          "*": "ItemSet.&.Account"
        },
        "ClaimedAmount": {
          "*": "ItemSet.&.ClaimedAmount"
        }
      }
    }
}

]