JSON TO CSV Conversion In Mule 4
In this tutorial we will demonstrate how can we perform json to csv conversion in mule 4 and perform some more operations while converting to CSV
UseCase-1
Sample JSON
{ "First Name": "John", "Last Name": "Right", "Sex": "Male", "EmployeeID": "123", "email_address": "[email protected]", "country": "India", "date_of_birth": "17-07-1966" }
Create a sample mule application
Write Transformation to convert json to csv
%dw 2.0 output application/csv quoteValues=true --- Payload
Write file to local directory
Deploy the application and test the flow
CSV File is created in Local Directory
UseCase-2
Now if we don’t want to write the Header we need to write the transform message as shown below
%dw 2.0 output application/csv quoteValues=true,header=false --- Payload
Deploy the application and see the result below:
UseCase-3
Now if we want to change the separator, use the below transformation
%dw 2.0 output application/csv quoteValues=true,header=true,separator="|" --- Payload
Deploy the application and see the result after opening in Text Pad
thank you for this post, it helped me finish my job quickly
First Name|Last Name|Sex|EmployeeID|email_address|country|date_of_birth
“John”|”Right”|”Male”|”123″|”[email protected]”|”India”|”17-07-1966”
How to convert this csv format back to json ?
As a complete beginner, I found that when converting a json payload from a HTTP Request to CSV, I would get binary errors. I resolved this by changing the MIME Type from blank to Application/json in the HTTP Requestor.
worked thankyou!
Hi My requirement is to read DB records (6000) and process them in batch (during the processing call multiple rest apis) and write all the records in CSV.
The problem I am facing is I need to write headers only once during the batch processing since I am appending the records in the file.
I tried setting a counter variable but during each parallel run the counter is initiating again.
Please suggest if you know any alternate