簡體   English   中英

DataWeave 函數

[英]DataWeave Functions

這是我的 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"
            }
        }
    }
}

為了避免在有效負載中映射其他字段,您可以嘗試這個 -

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

它首先獲取有效負載中除詳細信息之外的所有內容,然后通過排除 dcsId 添加詳細信息。

哼!

試試這個

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

希望這會有所幫助。

這是剛剛刪除 dcsId 的表達式

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

試試這個。

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

腳本

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

這是一個遞歸函數,用於從任何級別的 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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM