如何使用 Dataweave 将纯文本字符串输出到 Anypoint Platform 上的变量?

How do I output a plain text string to a variable on Anypoint Platform with Dataweave?

我觉得自己快要疯了,因为我想不出如何做像遍历对象、连接纯文本字符串并将结果输出到变量这样简单的事情。这是我做过的类似的东西,效果很好:

%dw 2.0
output application/xml
var promptParams = attributes.queryParams filterObject($$ startsWith "PROMPT") orderBy($$)
---
{
RESULT: {
    Prompts: 
        promptParams mapObject(promptValue, promptName, index) -> {
            PROMPT: {
                UniquePromptName: promptName,
                FieldValue: promptValue
                }
        }
    }
}

所以在这种情况下,我过滤 url 查询字符串参数以仅获取我想要的参数,然后迭代这些参数并构造 xml 输出。我遇到的问题是,如果我尝试做同样的事情,但将纯文本字符串输出到变量,我将无法工作。

基本上我想要的是从这个输入开始:

https://example.com?PROMPT1=foo&PROMPT2=bar&PROMPT3=lorem&PROMPT4=ipsum&utm_source=Dolor&utm_campaign=SitAmet

将此输出存储在流变量中:

foo!bar!lorem!ipsum

我一定是遗漏了一些基本的东西,因为完成它并不难。我做错了什么?

应该是这样的:

%dw 2.0
output text/plain
var promptParams = attributes.queryParams filterObject($$ startsWith "PROMPT")
---
promptParams pluck($) reduce ($$ ++ "!" ++ $)

输出: foo!bar!lorem!ipsum

您要求的是纯文本,但如果您在流程中使用变量,我会推荐 application/java。

%dw 2.0
output text/plain
var promptParams = (((payload.message splitBy "?")[1]) splitBy "&") //stored url //in payload.message
---
promptParams map {
    a: ($ splitBy "=")[1]
}.a joinBy "!"

您可以使用 pluckjoinBy,如果您使用的是转换消息组件,请确保将目标设置为变量。

<ee:transform doc:name="Transform Message" >
    <ee:variables >
        <ee:set-variable variableName="promptAttributes" ><![CDATA[%dw 2.0
output text/plain
---
(attributes.queryParams[?($$ startsWith "PROMPT")] pluck $) joinBy "!"]] </ee:set-variable>
    </ee:variables>
</ee:transform>