Reputation: 421
I am using mule 4.4 Community edition and my requirement is to transform a JSON payload into pipe delimited data that needs to be written to a file
Incoming JSON payload is like below :
{
"inputBy": "Bill",
"college": {
"id": 21,
"name": "Assisi Centre of Excellence",
"contact": {
"name": "John Doe",
"phone": "123456789",
"email": "[email protected]"
}
},
"students": [
{
"studentId": 1,
"name": "Jim",
"age": 19,
"year": 12
},
{
"studentId": 2,
"name": "Mark",
"age": 18,
"year": 11
}
]
}
I then need to convert it into pipe delimited data as below :
Header|Bill|21|Assisi Centre of Excellence|John Doe|123456789|[email protected]|||
Data|1|Jim|19|12
Data|2|Mark|18|11
From JSON the student data is an array that needs to go in the file as details while rest of the JSON needs to go as the first line in the file ( Header )
To achieve this I was trying to define a variable where using simple concatenation I was creating the header separately .
<set-variable value='#["Header|" ++ payload.inputBy ++ "|" ++ payload.college.id ++ "|" ++ payload.college.name ++ "|" ++ payload.college.contact.name ++ "|" ++ payload.college.contact.phone ++ "|" ++ payload.college.contact.email ++ "|||" ]' doc:name="Set emp Header" variableName="myHdr"/>
So the variable myHdr
has this value:
Header|Bill|21|Assisi Centre of Excellence|John Doe|123456789|[email protected]|||
Then in a separate component ( setPayload) was traversing through 'student' array
<set-payload value='#[%dw 2.0
output application/csv header=false, separator="|"
---
payload.students]' doc:name="Transform emp data" />
This generated following payload :
1|Jim|19|12
2|Mark|18|11
So now I have the header data in a variable : myHdr
and the actual student data in payload
Before writing this to a file was thinking of stitching the two together so tried to do it in a logger :
<logger level="INFO" doc:name="Logger" category="test" message="#[vars.myHdr ++ payload]"/>
and now I get the error :
""You called the function '++' with these arguments: 1: String ... 2: Array ([{
But it expects one of these combinations: (Array, Array) (String, String)
So I am assuming Dataweave is complaining since the header data is a String while the payload is an array Do I need to convert the payload ( array ) into a string ? and how do I do it ?
Upvotes: 0
Views: 1453
Reputation: 25837
It is not a good approach to output a CSV, JSON or XMl and then try to concatenate as string. It is better to use the native representation (Java) to construct the structure and the output in the desired end format.
Construct the header as an object with any keys, and the values are the header names. Put that object into an array, then concatenate the array of data. Then you can output the complete array as CSV, with automatic headers disabled.
Example:
%dw 2.0
output application/csv header=false, separator="|"
var headers=[{
h1: "Header",
h2: payload.inputBy,
h3: payload.college.id,
h4: payload.college.name,
h5: payload.college.contact.name,
h6: payload.college.contact.phone,
h7: payload.college.contact.email,
h8: "",
h9: "",
h10: ""
}]
---
headers ++ payload.students
Output:
Header|Bill|21|Assisi Centre of Excellence|John Doe|123456789|[email protected]|||
1|Jim|19|12
2|Mark|18|11
Upvotes: 2