简体   繁体   中英

DataWeave 2.0 Backslash escaping

How can you get a single backslash in DataWeave 2.0?

%dw 2.0
output application/json
---
{
  "attempt1": "\String",
  "attempt2": "\\String"
}

Returns:

{
  "attempt1": "\\String",
  "attempt2": "\\String"
}

Looks like your output is a json and in json the \\ always need to be escaped inside a string. That is why you are always going to see two \\

It appears it will always resolve to an even number if the output is in json like @machaval has said.

Tweaking mulesoft documentation: https://docs.mulesoft.com/mule-runtime/4.1/dataweave-language-introduction#examples

%dw 2.0
output application/json
---
{
    "a": "something",
    "b": "dollar sign (\$)",
    "c": 'single quote (\')',
    "c": "double quote (\")",
    "e": 'backtick (`)',
    "f": "backslash(\)",
    "g": "backslashOdd(\\\)",
    "h": "backslashEven(\\\\\\)"
}

produces:

{
  "a": "something",
  "b": "dollar sign ($)",
  "c": "single quote (')",
  "c": "double quote (\")",
  "e": "backtick (`)",
  "f": "backslash(\\)",
  "g": "backslashOdd(\\)",
  "h": "backslashEven(\\\\)"
}

Favoriting this question in case you get a better answer. I think I ended up using toString().replace() in the past

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM