Search code examples
dataweavemulesoft

JSON transformation via Dataweave


I have an input JSON below which I want to transform it into another JSON structure:

Input JSON

{
    data={
       
            schema=81Ze2hDYGKOQYW02LVtUMQ,
            payload={
                        value__c=500,
                        reference__c=00001503PM,
                        CreatedById=0051x000003SQ9eAAG,
                        originalReference__c=882595596510490G,
                        currency__c=SEK,
                        CreatedDate=2020-09-10T15:16:57.175Z,
                        merchantAccount__c=Hastens_eCom_Test
                    },
            event={replayId=18188}
        },
            channel=/event/Capturepayment__e
}

and I want to transform it into the format below.

Output JSON

   {
      "originalReference": "882595596510490G",
      "modificationAmount": {
        "value": 500,
        "currency": "SEK"
      },
      "reference": "00001503PM",
      "merchantAccount": "Hastens_eCom_Test"
    }

Solution

  • Your input payload doesn't seem to be a valid JSON.

    Using the input payload adjusted to be a valid JSON:

    {
       "data":{
          "schema":"81Ze2hDYGKOQYW02LVtUMQ",
          "payload":{
             "value__c":500,
             "reference__c":"00001503PM",
             "CreatedById":"0051x000003SQ9eAAG",
             "originalReference__c":"882595596510490G",
             "currency__c":"SEK",
             "CreatedDate":"2020-09-10T15:16:57.175Z",
             "merchantAccount__c":"Hastens_eCom_Test"
          },
          "event":{
             "replayId":"18188"
          }
       },
       "channel":"/event/Capturepayment__e"
    }
    

    the following dataweave expression:

    %dw 2.0
    output application/json
    ---
    {
      originalReference: payload.data.payload.originalReference__c,
      modificationAmount: {
        value: payload.data.payload.value__c,
        currency: payload.data.payload.currency__c,
      },
      reference: payload.data.payload.reference__c,
      merchantAccount: payload.data.payload.merchantAccount__c
    }
    

    will result in the expected output:

    {
      "originalReference": "882595596510490G",
      "modificationAmount": {
        "value": 500,
        "currency": "SEK"
      },
      "reference": "00001503PM",
      "merchantAccount": "Hastens_eCom_Test"
    }