【问题标题】:Need to get the desired output from the input - DataWeave需要从输入中获得所需的输出 - DataWeave
【发布时间】:2023-04-04 03:38:01
【问题描述】:

在以下情况下苦苦挣扎。任何指导/意见将不胜感激。

输入负载:

{
    "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"
}

预期输出:

{
  "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"
      }
    ]
}

输入中的字段数量可能会有所不同,或者某些字段可能不存在; 数据集、合作伙伴名称、guiid 也有可能在输入中不存在。在这种情况下,只需要显示可用字段,即

{
      "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"
          }
        ]
}

【问题讨论】:

    标签: dataweave mulesoft mule4


    【解决方案1】:

    我已将字符串函数用于子字符串功能。

    %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))
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-20
      • 1970-01-01
      • 2013-10-16
      • 2021-05-24
      • 1970-01-01
      相关资源
      最近更新 更多