1
votes

I am reading through the Dataweave documentation and am stumped with the example below.

The documentation says

Input directives allow you to make any number of input sources available in global variables, which can then be referenced in any part of the Transform’s body. To reference one of these, you can just call it by the name you defined in the directive.

Then follows up with the example below

%dw 1.0
%input in0
%output application/xml
---
payload

My questions:

In which scopes will mule look for the variable in0? Payload, Flow, Session or something else, and in which order?

In this example, where is in0 used? How does it help in this example?

Why would I need an input variable? Dataweave seems to allow flowVars.hello.

2

2 Answers

2
votes

I think the example doesn't match the text. That example should be:

%dw 1.0
%input in0
%output application/xml
---
in0

By default the input (however you named it) will be the payload. You can just be explicit and optionally also define the payload as an input directive in the header, although this isn’t required.

%dw 1.0
%input payload application/xml
%output application/xml
---
{
  a: payload
}

Further in the documentation there are better examples:

CSV Input Directive When defining an input of type CSV, there are a few optional parameters you can add to the input directive to customize how the data will be parsed.

header: boolean that defines if the first line in the data contains headers

separatorChar: character that separates fields, ',' by default

quoteChar: character that defines quoted text, " " by default

escapeChar: character that escapes quotes, / by default

A CSV input directive with custom parameters set looks like this:

%input in0 application/csv header=true separatorChar=; When header=true you can then access the fields within the input anywhere by name. Ex: in0.userName.

When header=false you must access the fields by index, referencing first the entry and then the field, Ex: in0[107][2]

0
votes

Generally, you would not need to declare anything with %input directive. By default payload, flowVars, inbound/outbound properties, sessionVars etc are accessible in dataweave like they are in MEL.

MEL is same as Java, anything that you can write in java to debug (System.out) you can do it in MEL too.

%dw 1.0
%output application/java
---
{
    GENDER: payload.gender,
    test:flowVars.test,
    testIp:inboundProperties.testIp,
    testOp:outboundProperties.testOp
}

Look at this example for multiple inputs - Multiple Inputs