0
votes

I have an input like:

[
    {
        "Attachments": [
            {
                "FileType": "pdf"
            },
            {
                "FileType": "txt"
            }
        ],
        "Name": "test"
    },
    {
        "Attachments": [],
        "Name": "test2"
    }
]

]

and the output I want is:

[
    {
        "FileType": "pdf",
        "Name": "test"
    },
    {
        "FileType": "txt",
        "Name": "test"
    },
    {
        "FileType": null,
        "Name": "test2"
    }
]

So, the question is how can I get an array with an element for each combination of attachment and name using DataWeave 2.0?

1

1 Answers

3
votes

This script does, though I wonder if it could be simpler:

%dw 2.0
output application/json
---
flatten(
    payload map ((item) -> 
        if (!isEmpty(item.Attachments)) 
            item.Attachments map ((value) -> 
                {
                    Name: item.Name,
                    FileType: value.FileType
                }
            ) 
        else {
            Name: item.Name,
            FileType: "null"
        } 
    )
)

Input:

[
    {
        "Name": "test",
        "Attachments": [{ "FileType": "pdf" }, { "FileType": "txt" }] 
    },{ 
        "Name": "test2", 
        "Attachments": [] 
    }
]

Output:

[
  {
    "Name": "test",
    "FileType": "pdf"
  },
  {
    "Name": "test",
    "FileType": "txt"
  },
  {
    "Name": "test2",
    "FileType": "null"
  }
]