Search code examples
dictionaryanypoint-studiomulesoft

Map to an array within an object within an array


I am still having trouble understanding how to use the map function. In this case my payload is a JSON object that contains an array of "orders" with each "order" being an object... How do I create a map that would let me get to the array of "ContactEmailAddresses"?

{
    "orders": [
        {
            "OrderGroupNumber": 1,
            "Requester": {
                "Name": "Mickey Mouse"
            },
            "ContactEmailAddresses": [
                "user1@abc.com",
                "user2@abc.com"
            ],
            "CreatedByEmailAddress": "user1@abc.com"
        },
        {
            "OrderGroupNumber": 2,
            "Requester": {
                "Name": "Donald Duck"
            },
            "ContactEmailAddresses": [
                "user3@abc.com",
                "user4@abc.com"
            ],
            "CreatedByEmailAddress": "user3@abc.com"
        },
        {
            "OrderGroupNumber": 3,
            "Requester": {
                "Name": "Goofy"
            },
            "ContactEmailAddresses": [
                "user5@abc.com",
                "user6@abc.com"
            ]
        }
    ]
}

My current attempt that doesn't work is:

payload.*orders map (order, index) ->
{
    order.contactEmailAddresses
}

Solution

  • %dw 2.0
    output application/json
    ---
    payload.orders flatMap $.ContactEmailAddresses
    

    Output:

    [
      "user1@abc.com",
      "user2@abc.com",
      "user3@abc.com",
      "user4@abc.com",
      "user5@abc.com",
      "user6@abc.com"
    ]