0

Is there easy way to convert CSV into JSON in mule 4? Currently I'm doing it as below.

 %dw 2.0
    output application/json
    ---
    (payload splitBy('\r\n')) map using( tmp = $ splitBy(',')) {
             id : tmp[0],
             name: tmp[1]
    }
2
  • use AnupamBhusari suggestion but you need to set the metadata of the input as CSV. Please refer to this link: docs.mulesoft.com/anypoint-studio/v/6/… Commented Jun 12, 2018 at 15:07
  • is you file on windows machine as you are replacing \r\n on it? I am getting same issue too. Below answers doesn't work Commented Jul 13, 2021 at 1:06

4 Answers 4

2

Try with following

%dw 2.0
output application/json
---
payload

Input :-

id,name
2,Tom
3,Jerry

And output produced is

[
  {
    "id": "2",
    "name": "Tom"
  },
  {
    "id": "3",
    "name": "Jerry"
  }
]

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

Best working solution, if you have a CSV with comma separated values and a first row with header is:

FIRST TRANSFORM MESSAGE

%dw 2.0
output application/csv headerLineNumber=0, header=true
---
payload

SECOND TRANSFORM MESSAGE

%dw 2.0
output application/dw
---

payload

Comments

0

Try below code, you can adjust the code according to your need

`

%dw 2.0
input payload csv escape=","
output application/json
---
payload

`

Comments

-1

Try following in Transform message

%dw 2.0

output application/json

payload map { FirstName: $.FirstName, LastName : $.LastName, Department : $.Department, Email : $.Email, Phone : $.Phone, CreatedDate : $.CreatedDate }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.