0

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
&#10;output application/csv header=false, separator="|"
&#10;---
&#10;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 ?

aled
  • 21,330
  • 3
  • 27
  • 34

1 Answers1

2

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|john@doe.com|||
1|Jim|19|12
2|Mark|18|11
aled
  • 21,330
  • 3
  • 27
  • 34
  • thanks @aled , in my case the header has a different number of fields and details has a different number . My understanding is dataweave will need an array / list to work on which will not be possible with the header data ... – GettingStarted With123 Jun 10 '22 at 03:39
  • Works for me. I added the example to my answer. – aled Jun 10 '22 at 13:39