Mule-Newbie
Mule-Newbie

Reputation: 107

Need to get the desired output from the input - DataWeave

Struggling with the below scenario. Any guidance/inputs would be appreciated.

Input payload:

{
    "message": "Bad request :/data/1/actualDelivery expected type: String, found: Null\n
                            /data/1/propertyName expected type: String, found: Null\n
                            /data/1/uniqueID expected type: String, found: Null\n
                            /data/0/amortizationSchedule expected type: String, found: Null\n
                            /data/0/uniqueID expected type: String, found: Null\n
                            /metadata/guid expected type: String, found: Null\n
                            /metadata/partnerName expected type: String, found: Null\n
                            /datasetName expected type: String, found: Null"
}

Expected output:

{
  "datasetName":" expected type: String, found: Null"
  "metadata":{
      "partnerName":" expected type: String, found: Null",
      "guid" : " expected type: String, found: Null"
    },
   "data": [
      {
        "uniqueID" : " expected type: String, found: Null",
        "amortizationSchedule":" expected type: String, found: Null",
      },
      {
        "uniqueID" : " expected type: String, found: Null",
        "propertyName":" expected type: String, found: Null",
        "actualDelivery": " expected type: String, found: Null"
      }
    ]
}

Number of fields in input might vary or some fields might not be there; There is also a possibility that dataset, partner name, guiid might not be there in input. In that case only available fields needs to be displayed i.e

{
      "data": [
          {
            "uniqueID" : " expected type: String, found: Null",
            "amortizationSchedule":" expected type: String, found: Null",
          },
          {
            "uniqueID" : " expected type: String, found: Null",
            "propertyName":" expected type: String, found: Null",
            "actualDelivery": " expected type: String, found: Null"
          }
        ]
}

Upvotes: 0

Views: 264

Answers (1)

sudhish_s
sudhish_s

Reputation: 628

I have used String functions for substring capabilities.

%dw 2.0
import * from dw::core::Strings
output application/json

var elms = ((substringAfter(payload.message, ":")) splitBy "\n")
var dataElms = elms filter ($ startsWith "/data/")
var metadataElms = elms filter ($ startsWith "/metadata/")
var datasetElms = elms filter ($ startsWith "/datasetName")
var splitDataElms = dataElms distinctBy ($ startsWith "/data/0") map do {
    var dataIndex = "/data/" ++ substringBefore(substringAfter($, "/data/"), "/")
    ---
    (dataElms filter ($ startsWith dataIndex))
}

fun mapElms (elmMap) = elmMap map do {
    var name = substringBefore(substringAfterLast($, "/"), " ")
    var value = substringAfterLast ($, name)
    ---
    (name): value
} reduce ($++$$)

fun isNotEmpty (elm) = not isEmpty(elm)
---
{
    (datasetName: substringAfter(datasetElms[0], "/datasetName")) if (isNotEmpty(datasetElms)),
    (metadata: mapElms(metadataElms)) if (isNotEmpty(metadataElms)),
    data: splitDataElms map (dataElm) -> (mapElms(dataElm))
}

Upvotes: 1

Related Questions