[英]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.