简体   繁体   中英

DataWeave Functions

This is my JSON request and I want to remove dcsId field from my JSON payload in DataWeave transformation.

How can I do that?

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

To avoid mapping of other fields in payload, you can try this -

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

It first gets everything in payload excepts details then add details by excluding dcsId.

HTH!

Try this

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

Hope this helps.

这是刚刚删除 dcsId 的表达式

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

try this.

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

Script

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

Output

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

Here is a recursive function to remove a key from any level of json payload

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM