Search code examples
mappingdataweavemulesoft

Dataweave problem with mapping from message to another message


This is my input Json format

{
  "P2": {
    "queue": {
      "tst-bp-1": {
        "smsTo": "John Test1: 06-12345678, Emilo Test1: 06-87654321",
        "mailTo": "[email protected], [email protected]"
      },
      "tst-bp-2": {
        "smsTo": "John Test2: 06-12345678, Emilo Test2: 06-87654321",
        "mailTo": "[email protected], [email protected]"
      }
    },
    "host": {
      "[email protected]": {
        "smsTo": "Jeff Test1: 06-12345678, Tim Test1: 06-87654321",
        "mailTo": "Jeff [email protected], [email protected]"
      },
      "[email protected]": {
        "smsTo": "Jeff Test1: 06-12345678, Tim Test1: 06-87654321",
        "mailTo": "Jeff [email protected], [email protected]"
      }
    },
    "cert": {
      "[email protected]": {
        "smsTo": "Jeff Test1: 06-12345678, Tim Test1: 06-87654321",
        "mailTo": "Jeff [email protected], [email protected]"
      }
    }
  },
  "P3": {
    "queue": {
      "acc-bp-1": {
        "smsTo": "John Test1: 06-12345678, Emilo Test1: 06-87654321",
        "mailTo": "[email protected], [email protected]"
      }
    },
    "host": {
      "[email protected]": {
        "smsTo": "Jeff Test1: 06-12345678, Tim Test1: 06-87654321",
        "mailTo": "Jeff [email protected], [email protected]"
      }
    },
    "etc": null
  }
}

And this must be the result:

[
  {
    "systemName": "tst-bp-1",
    "priority": "P2",
    "systemType": "queue"
    "smsTo": "John Test1: 06-12345678, Emilo Test1: 06-87654321",
    "mailTo: "[email protected], [email protected]"
  },
  {
    "systemName": "tst-bp-2",
    "priority": "P2",
    "systemType": "queue",
    "smsTo": "John Test2: 06-12345678, Emilo Test2: 06-87654321",
    "mailTo: "[email protected], [email protected]"
  },
  {
    "systemName": "[email protected]",
    "priority": "P2",
    "systemType": "host",
    "smsTo": "Jeff Test1: 06-12345678, Tim Test1: 06-87654321",
    "mailTo: "Jeff [email protected], [email protected]"
  },
  {
    "systemName": "[email protected]",
    "priority": "P2",
    "systemType": "host",
    "smsTo": "Jeff Test1: 06-12345678, Tim Test1: 06-87654321",
    "mailTo: "Jeff [email protected], [email protected]"
  },
  {
    "systemName": "[email protected]",
    "priority": "P2",
    "systemType": "cert",
    "smsTo": "Jeff Test1: 06-12345678, Tim Test1: 06-87654321",
    "mailTo: "Jeff [email protected], [email protected]"
  },
  [
  {
    "systemName": "acc-bp-1",
    "priority": "P3",
    "systemType": "queue"
    "smsTo": "John Test1: 06-12345678, Emilo Test1: 06-87654321",
    "mailTo: "[email protected], [email protected]"
  },
  {
    "systemName": "[email protected]",
    "priority": "P3",
    "systemType": "host",
    "smsTo": "Jeff Test1: 06-12345678, Tim Test1: 06-87654321",
    "mailTo: "Jeff [email protected], [email protected]"
  }
] 

I have some things tried like this:

flatten(payload pluck (priority, systemType) -> 
    systemType pluck ((systemName, systemData) -> {
        systemName: systemName,
        priority: priority,
        systemType: systemType,
        smsTo: systemData."smsTo",
        mailTo: systemData."mailTo"
    })
)

But the code is not working. Help me please. The problem is I guess to flatten this array to the wanted object. Well I have to add more details. But there is not much more detail, so I have tried the code in de playground of dataweave


Solution

  • When dealing with such problems, you can try to first pick each "parts" of payload one by one using a combination of entriesOf and flatMaps and then just map it to the final output. The flatMap will take care of the nested arrays that entriesOf will create. You can also use pluck with flattens but it will require you to keep track of nested brackets and could make the code harder to read in my opinion. But if you find like that better, you can convert the following easily to the other version

    %dw 2.0
    output application/json skipNullOn = "arrays" //skip null elements in array to remove the entries like "etc": null
    ---
    entriesOf(payload) flatMap ((priority) -> 
        entriesOf(priority.value) flatMap ((systemType) -> 
            entriesOf(systemType.value) flatMap ((systemCommunication) -> (
                {
                    systemName: systemCommunication.key,
                    priority: priority.key,
                    systemType: systemType.key,
                    smsTo: systemCommunication.value.smsTo,
                    mailTo: systemCommunication.value.mailTo,
                }
            ))
        )
    )