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": "john@doe.com"
}
},
"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|john@doe.com|||
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|john@doe.com|||
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 ?