Search code examples
muleanypoint-studiodataweave

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


I feel like I'm losing my mind because I can't figure out how to do something as simple as iterate over an object, concatenate a plain text string, and output the result to a variable. Here is something similar I've done which works fine:

%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
                }
        }
    }
}

So in this case I filter the url query string parameters to get only the ones I want, then I iterate over those and construct the xml output. The problem I'm having is that if I try to do the same thing but output a plain text string to a variable, I can't get anything to work.

Basically what I want is to go from this input:

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

to this output stored in a flow variable:

foo!bar!lorem!ipsum

I must be missing something basic because it can't be that hard to accomplish this. What am I doing wrong?


Solution

  • It should be something like this:

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

    Output: foo!bar!lorem!ipsum

    You asked for text plain, but I would recommend application/java if you are usign the variable inside the flow.