0

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 Answer 1

3

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"
  }
]
Sign up to request clarification or add additional context in comments.

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.