3

...I'm using Anypoint Studio 7.6 and writing in DataWeave 2.0

I cannot find the definitive way to include the $ (dollar sign) character in a quoted string.

Here's what I tried:

%dw 2.0
output application/dw
var sign = "\u0024" // unicode dollar sign
type currency = String {format: "$sign ###.00"} // interpolation from previous var
var cost = 100 as currency
---
{
    directly: "a dollar sign like this: \$",
    asdefined: sign,
    indirectly: "This flight, costs $(cost), and is operated by " 
       ++ payload.airline
}

Here's what I got for my trouble:

{
  directly: "a dollar sign like this: \$",
  asdefined: "\$",
  indirectly: "This flight, costs \$ 100.00, and is operated by United"
}

I feel like I'm missing something simple.

agentv
  • 739
  • 1
  • 9
  • 21

2 Answers2

6

Hi There is no problem with what you see it is just that you are using application/dw as output. In that format the $ need to be escaped that is why you see it like that. If you change to application/json they will disappear.

machaval
  • 4,969
  • 14
  • 20
  • ...that is so helpful! And hilarious. I always tell people to use application/dw while coding because it's mostly forgiving of data format errors. But I should have considered that it would try to show me a little too much. Thanks! – agentv Sep 09 '20 at 19:58
  • 2
    Just for the record, application/dw should only be used for troubleshooting, not as a 'standard' format. It has a performance and compatibility impact. Using it by default has the risk of forgetting it is there, and it looks similar enough to JSON that it is not obvious. – aled Sep 09 '20 at 20:02
3

This may be just because your output is application/dw. Once you switch to application/json or other mime-type the escaping should work correctly.

gonoz
  • 31
  • 1