GettingStarted With123
GettingStarted With123

Reputation: 427

reading a file in mule and parsing with dataweave

I am using Mule 4.4 and am trying to read a file and then convert into JSON using Dataweave. Here is the file ( it has no headers ) and last line in file is blank

abc|Milk|3.9|
lmn|Butter|5.5|
xyz|Bread|1.6|

while reading the file have set mime type as application/csv ( though its pipe delimited ) , will that be a problem ?

The problem I am encountering is , I want to transform the file content into json like below:

[
 {
   "id": "abc",
   "product": "Milk",
   "price": 3.9
 },
{
"id": "lmn",
   "product": "Butter",
   "price": 5.5
 },
etc

]

However it is showing up like below : ( with the first row repeating )

[
 {
   "id": {
     "abc|Milk|3.9|": "lmn|Butter|5.5|"
    }
 },
{
"id": {
  "abc|Milk|3.9|": "xyz|Bread|1.6|"
}
}
]

I think this is happening since mule is assuming first row to contain a header . Here is my dataweave :

%dw 2.0
output application/json

---

payload map (value,index)->
{
   id:value
}

Upvotes: 0

Views: 2264

Answers (2)

Harsha Shivamurthy
Harsha Shivamurthy

Reputation: 467

Please try as below.

%dw 2.0
input payload application/csv separator="|", header = false
output application/json
---
payload map (value,index)->
{
    id:value[0],
    product: value[1],
    price: value[2]
}

enter image description here

Upvotes: 0

Michael Jones
Michael Jones

Reputation: 1910

It is showing up that way because you have it set in such a way that it considers the first row to be your header row. You need to set the header value for the reader to false so that it understands that the first row is not, in fact, a header.

In addition to that, you will need to set the separator to the pipe character.

For example, if you read it in as text and processed it purely using dataweave, it would look like:

%dw 2.0
output application/json
---
read(payload, "applicatin/csv", {"header": false, "separator": "|" }) map (
    {
        id: $[0],
        product: $[1],
        price: $[2]
    }
)

enter image description here

Upvotes: 1

Related Questions