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

JSON TO CSV Conversion In Mule 4

 

Write Transformation to convert json to csv

%dw 2.0
output application/csv quoteValues=true
---
Payload

 

JSON TO CSV Conversion In Mule 4

 

Write file to local directory

JSON TO CSV Conversion In Mule 4

 

Deploy the application and test the flow

JSON TO CSV Conversion In Mule 4

 

CSV File is created in Local Directory

JSON TO CSV Conversion In Mule 4

 

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 taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Dileep Sathupati
Dileep Sathupati
2 years ago

thank you for this post, it helped me finish my job quickly

phani
phani
1 year ago

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 ?

Paul
Paul
1 year ago

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.

Srimukhi Kasula
Srimukhi Kasula
1 year ago

worked thankyou!

Sravani G
Sravani G
1 year ago

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