Search code examples
muledataweavemulesoftanypoint-studiomule4

Need to convert the payload to the desired output


In the below array there can be multiple objects, and each object can have more than below mentioned key values. I need only the uniqueID and name fields to be displayed.

[
      {
        "uniqueID": "1",
        "Name": "Annie",
        "Standard": "3",
        "School" : "ABC School"
      },
      {
        "uniqueID": "2",
        "Name": "Apoo",
        "Standard": "4",
        "School" : "PQR School"
      },
      {
        "uniqueID": "3",
        "Name": "Xavier",
        "Standard": "5",
        "School" : "MNO School"
      }
]

Desired output:

{
    "errors": [
        {
            "uniqueID": "1",
            "Name": "Annie"
        },
        {
            "uniqueID": "2",
            "Name": "Apoo"
        },
        {
            "uniqueID": "3",
            "Name": "Xavier"
        }
    ]
}

Solution

  • Couple of approaches.

    Script

    %dw 2.0
    output application/json
    ---
    errors: payload map {
        uniqueID: $.uniqueID,
        Name: $.Name
    }
    
    %dw 2.0
    output application/json
    ---
    errors: payload map {
        ($ - "Standard" - "School")
    }