-1

I have a data weave transformation converting an excel file to JSON, I had to change the value of an element (column of the file) as per the key-value pair values stored in a variable.

Please let me know how to achieve this.

Below is my data weave which converts 600 rows in the file to a JSON. However, I need to change the value for Brand as per the key-value pair mapping I stored in a variable.

%dw 2.0
output application/json 
---
payload map(payload01,index01)->{
  city: payload01.City,
  province: payload01.Province,
  phone: payload01.Phone,
  fax: payload01.FAX,
  email: payload01.EMAIL,
  Brand: payload01.'Fuel Brand'
}
Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42
andy
  • 1
  • 1

1 Answers1

2

I understand that you want to use the value in attribute 'Fuel Brand' of the input payload to be used as the index in a variable:

%dw 2.0
output application/json
---
payload."Sheet Name" map(payload01,index01)-> { 
    city: payload01.City, 
    ...
    Brand: vars.brandsMapping[payload01.'Fuel Brand']
}

For example if the input is (note: I removed other attributes to simplify the example):

[{City=City A, Fuel Brand=brand1}, {City=City B, Fuel Brand=brand3}]

And the variable vars.brandsMapping contains:

{brand1=The brand1, brand2=The brand2, brand3=The brand3}

The output will be:

[
  {
    "city": "City A",
    "Brand": "The brand1"
  },
  {
    "city": "City B",
    "Brand": "The brand3"
  }
]

UPDATE: Since you clarified that you want to a dynamic mapping, the method that can be used for that is mentioned at the documentation page: https://docs.mulesoft.com/mule-runtime/4.1/dataweave-cookbook-map-based-on-an-external-definition

aled
  • 21,330
  • 3
  • 27
  • 34
  • Since the input is an excel, you need to reference the sheet first – Shoki Sep 30 '19 at 14:17
  • I have below json in the vars.brandsMapping [ { "data_input": "ULTRA", "transformed_value": "ULTIMA" }, { "data_input": "PETA", "transformed_value": "Petro" }, { "data_input": "SHE", "transformed_value": "Shelly" } ] and If I get Fuel Brand = ULTRA in the sheet I need to map as per brand vars , in this case i need to transform Fuel Brand = ULTRA dynamically to all rows in the sheet – andy Oct 01 '19 at 03:17
  • There is a different method to do that. I'll update my answer. – aled Oct 01 '19 at 03:30