1

I'm totally new on transforming JSON to CSV in Mulesoft. Currently i need to work on to transform a nested JSON into CSV which my CSV should look like this CSV and my JSON will be like

{
  "Invoice": [
    {
      "Invoice Number*": "",
      "Supplier Name": "",
      "Supplier Number": "",
      "Status": "",
      "Invoice Date*": ""
    }
  ],
  "Invoice Line": [
    {
      "Invoice Number*": "",
      "Supplier Name": "",
      "Supplier Number": "",
      "Line Number": "",
      "Description*": "",
      "Supplier Part Number": "",
      "Auxiliary Part Number": "",
      "Price*": "",
      "Quantity": "",
      "Bulk Price": "",
      "Bulk Price Qty": ""
    }
  ],
  "Invoice Tax Line": [
    {
      "Invoice Number*": "",
      "Invoice Line Number": "",
      "Invoice Charge Number": "",
      "Line Number": "",
      "Tax Amount": "",
      "Tax Rate": "",
      "Tax Code": "",
      "Tax Rate Type": ""
    }
  ]
}

What i know about CSV is there will be only one header. What is the best way for me to map this complex JSON into CSV which will shows different header?

2

1 Answer 1

4

You can do something like this:

%dw 2.0
output application/csv header=false
---
flatten (
    [
        {"Invoice Number":"Invoice Number",
            "Supplier Name": "Supplier Name",
            "Supplier Number" :"Supplier Number",
            "Status" : "Status",
            "Invoice Date*" : "Invoice Date*"
        },
        {
            "Invoice Number":"Invoice Number*",
            "Supplier Name":"Supplier Name",
            "Supplier Number":"Supplier Number",
            "Status":"Status",
            "Invoice Date*":"Invoice Date*"
        },
        payload.Invoice map {
            "Invoice Number":$."Invoice Number*",
            "Supplier Name":$."Supplier Name",
            "Supplier Number":$."Supplier Number",
            "Status":$.Status,
            "Invoice Date*":$."Invoice Date*"
        }, 
        payload."Invoice Line" map {
            "Invoice Number": $."Invoice Number*",
            "Supplier Name":$."Supplier Name",
            ...
        } 
        , 
        payload."Invoice Tax Line" map {
            "Invoice Number":$."Invoice Number*",
            ...
        } 
    ]
)
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @aled, thanks for answering! Can you slightly explain what happening on this code?
The CSV support in DataWeave is meant to have 1 optional header line and the rest of the rows with the same structure. I'm abusing it a bit by creating each line by hand. For DataWeave there is no header. Because the items are an array the map for it can generate an array of objects, so we need to use the flatten() function to output a single array, which each object representing a line of the output.

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.