# How to do data mapping between two different payloads using datamapper

In this tutorial we are going to transform an incoming message payload to a different payload structure acceptable for the target system. The target systems expects the message in the following model.


{
    "VehicleRegDetails":[
        {
            "Owner_Name" : "Harry",
            "VehicleNo": "AT6783-00",
            "Location" : "NSW",
            "Car_Name" : "Honda",
            "Car_EngineNo" : "343-5993-99",
            "Car_Type" : "HatchBack"
        }
    ]
}

  1. TBD Following is the incoming payload message for this use case.

     [{
     	"VehicleNo": "AT6783-00",
     	"Location": "NSW",
     	"Owner": "Harry",
     	"Address": {
     		"Street": "Rosley St",
     		"Unit": 88,
     		"State": "NSW",
     		"PIN": "2134"
     	},
     	"VehicleInfo": {
     		"Type": "HatchBack",
     		"Name": "Honda",
     		"EngineNo": "343 - 5993 - 99"
     	}
     },
     {
     	"VehicleNo": "AQ888-00",
     	"Location": "VIC",
     	"Owner": "Dan",
     	"Address": {
     		"Street": "Redfern St",
     		"Unit": 12,
     		"State": "VIC",
     		"PIN": "2220"
     	},
     	"VehicleInfo": {
     		"Type": "HatchBack",
     		"Name": "KIA",
     		"EngineNo": "343 - 3333 - 99"
     	}
     },
     {
     	"VehicleNo": "AT9992-00",
     	"Location": "NSW",
     	"Owner": "Chris",
     	"Address": {
     		"Street": "Edmond St",
     		"Unit": 87,
     		"State": "NSW",
     		"PIN": "2123"
     	},
     	"VehicleInfo": {
     		"Type": "Sedan",
     		"Name": "BMW",
     		"EngineNo": "500 - 5003 - 99"
     	}
     },
     {
     	"VehicleNo": "AJ8990-00",
     	"Location": "WA",
     	"Owner": "Stanley",
     	"Address": {
     		"Street": "Ross St",
     		"Unit": 312,
     		"State": "WA",
     		"PIN": "3123"
     	},
     	"VehicleInfo": {
     		"Type": "HatchBack",
     		"Name": "Honda",
     		"EngineNo": "333 - 700 - 99"
     	}
     }
    ]
    
    
  2. Drag and JSON node from transformation section in the pallette to the canvas. Keep all the settings in JSON node as default.

  3. Wire the TBD to JSON node.

  4. Drag and Drop datamapper node from the transformation section in pallette to the canvas. Double click the datamapper node to open the editor.

    On the editor you should see a sample input form , a result form and on bottom a mapping form.

  5. On the sample input form paste the above given payload on step 1 and on the mapping form provide the following JSONata expression.

   {
       "VehicleRegDetails": $map($, function($v) {
        {
          "OwnerName" : $v.Owner,
          "VehicleNo": $v.VehicleNo,
          "Location" : $v.Address.State,
          "Car_Name" : $v.VehicleInfo.Type,
          "Car_EngineNo" : $v.VehicleInfo.EngineNo,
          "Car_Type" : $v.VehicleInfo.Type
        }
        
        })
}
  1. On the output you could see the expected preview.

  2. Click Save.

  3. Wire the JSON node to Datamapper node.

  4. Drop the debug node to the end of the flow and name it as Completed.

  5. Now click the TBD to test the flow.

Observation

The following output is printed on the debug panel.

{"VehicleRegDetails":[{"OwnerName":"Harry","VehicleNo":"AT6783-00","Location":"NSW","Car_Name":"HatchBack","Car_EngineNo":"343 - 5993 - 99","Car_Type":"HatchBack"},{"OwnerName":"Dan","VehicleNo":"AQ888-00","Location":"VIC","Car_Name":"HatchBack","Car_EngineNo":"343 - 3333 - 99","Car_Type":"HatchBack"},{"OwnerName":"Chris","VehicleNo":"AT9992-00","Location":"NSW","Car_Name":"Sedan","Car_EngineNo":"500 - 5003 - 99","Car_Type":"Sedan"},{"OwnerName":"Stanley","VehicleNo":"AJ8990-00","Location":"WA","Car_Name":"HatchBack","Car_EngineNo":"333 - 700 - 99","Car_Type":"HatchBack"}]}