【问题标题】:DataWeave FunctionsDataWeave 函数
【发布时间】:2021-04-02 01:20:33
【问题描述】:

这是我的 JSON 请求,我想在 DataWeave 转换中从我的 JSON 负载中删除 dcsId 字段。

我该怎么做?

{
    "status": "ok",
    "statusCode": "11011",
    "statusDescription": "Service: Get Profile ; Market: US ; Locale:en-US ; SourceId:DCS; ApiUid: 644e1dd7-2a7f-18fb-b8ed-ed78c3f92c2b; Description: The get profile call was successful.",
    "details": {
        "dcsId": "rfggrg",
        "marketCode": "US",
        "languageCode": "en",
        "profile": {
            "base": {
                "username": "abc",
                "firstName": "xc",
                "middleName": "test",
                "lastName": "123",
                "shortName": "xc",
                "displayName": "D",
                "suffix": "T",
                "prefix": "E"
            }
        }
    }
}

【问题讨论】:

    标签: mule dataweave


    【解决方案1】:

    为了避免在payload中映射其他字段,你可以试试这个-

    %dw 1.0
    %output application/json
    ---
    (payload - 'details') ++ (payload.details - 'dcsId')
    

    它首先获取有效负载中的所有内容,除了详细信息,然后通过排除 dcsId 添加详细信息。

    HTH!

    【讨论】:

    • 这个表达式除了删除dcsId外,还删除了details字段,并将details的所有字段提升一级。我不确定这是 OP 所描述的。
    【解决方案2】:

    这是删除 dcsId 的表达式

        (payload - 'details') ++ {details: payload.details - 'dcsId'}
    

    【讨论】:

    • 在我看来,这是正确的答案。表达得越简单,就越防弹。
    【解决方案3】:

    试试这个

    %dw 1.0
    %output application/json
    ---
    {
        status : payload.status,
        statusCode : payload.status,
        statusDescription : payload.statusDescription,
        details :  payload.details - 'dcsId' 
    }
    

    希望这会有所帮助。

    【讨论】:

      【解决方案4】:

      脚本

      %dw 2.0
      output application/json
      ---
          (payload - 'details') ++ {details: payload.details - 'dcsId'}
      

      输出

      {
        "status": "ok",
        "statusCode": "11011",
        "statusDescription": "Service: Get Profile ; Market: US ; Locale:en-US ; SourceId:DCS; ApiUid: 644e1dd7-2a7f-18fb-b8ed-ed78c3f92c2b; Description: The get profile call was successful.",
        "details": {
          "marketCode": "US",
          "languageCode": "en",
          "profile": {
            "base": {
              "username": "abc",
              "firstName": "xc",
              "middleName": "test",
              "lastName": "123",
              "shortName": "xc",
              "displayName": "D",
              "suffix": "T",
              "prefix": "E"
            }
          }
        }
      }
      

      【讨论】:

        【解决方案5】:

        试试这个。

        %dw 1.0
        %output application/json
        ---
        (payload - 'details') 
        ++ 
        details:(payload.details - 'dcsId')
        

        【讨论】:

          【解决方案6】:

          这是一个递归函数,用于从任何级别的 json 有效负载中删除密钥

          %dw 2.0
          output application/json
          var data = {
              "status": "ok",
              "statusCode": "11011",
              "statusDescription": "Service: Get Profile ; Market: US ; Locale:en-US ; SourceId:DCS; ApiUid: 644e1dd7-2a7f-18fb-b8ed-ed78c3f92c2b; Description: The get profile call was successful.",
              "details": {
                  "dcsId": "rfggrg",
                  "marketCode": "US",
                  "languageCode": "en",
                  "profile": {
                      "base": {
                          "username": "abc",
                          "firstName": "xc",
                          "middleName": "test",
                          "lastName": "123",
                          "shortName": "xc",
                          "displayName": "D",
                          "suffix": "T",
                          "prefix": "E"
                      }
                  }
              }
          }
          
          fun removeKey(val, keyToRemove) = val match {
              case is Array -> $ map ((v) -> removeKey(v, keyToRemove))
              case is Object -> ($ - keyToRemove) mapObject ((value, key, index) -> {(key): removeKey(value,keyToRemove)})
              else -> $
            }
          ---
          removeKey(data,"dcsId")
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-06-22
            • 2021-03-26
            • 2017-07-05
            • 2016-01-21
            • 1970-01-01
            • 2020-03-27
            • 1970-01-01
            • 2021-12-11
            相关资源
            最近更新 更多