0
votes

I am trying to convert the output of a database into json using transform message component.

This is the input payload

[{
    "usd ": 0.0, 
    "goal": 3041920,
    "Inr": 0.0,
}]

Data weave exp inside transformer

%dw 1.0
%output application/json
---
payload map ((payload01 , indexOfPayload01) -> {
    "usd ": payload01.usd ,
    "goal": payload01.goal,
    "Inr": payload01.Inr,

}

I have done the mapping in transform message and whenever testing the flow i am getting exception

com.mulesoft.weave.mule.exception.WeaveExecutionException

Can someone Help

Thanks, Nikhil

2
You should have added the details of the error and the exception. WeaveExecutionException by itself only says that there was an error. The details point to what was the error.aled
You need a closing ) and quotes around usd since the key has an additional space in there.Salim Khan

2 Answers

1
votes

The input JSON and the script have a few errors. First, the last comma in each attribute has to be removed. To access the "usd " key you need to use quotes in the key name. Having a quote in a key is weird anyway.

Corrected script:

%dw 1.0
%output application/json
---
payload map ((payload01 , indexOfPayload01) -> 
  { 
    "usd": payload01.'usd ', 
    "goal": payload01.goal, 
    "Inr": payload01.Inr
  })

Corrected input:

[{ "usd ": 0.0, "goal": 3041920, "Inr": 0.0 }]

Output:

[
  {
    "usd": 0.0,
    "goal": 3041920,
    "Inr": 0.0
  }
]
0
votes

You also don't quote the keys in dataweave template. See the samples here https://docs.mulesoft.com/mule-runtime/3.9/dataweave-language-introduction

%dw 1.0
%output application/json
---
payload map ((payload01 , indexOfPayload01) -> 
  { 
    usd: payload01.'usd ', 
    goal: payload01.goal, 
    Inr: payload01.Inr
  })