如何解决如何使用DataWeave更新运算符在嵌套的分层对象中屏蔽具有特定键名的所有值?
我正在尝试选择性地更新嵌套层次数据结构中的某些值。
我有嵌套的输入数据,例如
var flights =
{
"flights": {
"flight": {
"airline-name": "american","flight-code": "AA103","plane-type": null,"longitude": "33.94250107","price": "+750.00","destination": {
"airport-name": "los angeles international airport","city": "los angeles","altitude": "125","longitude":"33.94250107"
}
},"flight": {
"available-seats": "+18.00","airline-name": "delta","plane-type": "boeing 747","longitude": "33.94250107"
}
}
}
}
我想用一个case语句屏蔽所有级别的所有经度。
flights dw::util::Values::update {
case dest at .flights.*flight.*destination.longitude -> "******"
case ptype at .flights.*flight.*"plane-type" if((ptype ~= null)) -> "*******"
}
仅在目标值内匹配。
{
"flights": {
"flight": {
"airline-name": "american","plane-type": "*******","longitude": "******"
}
},"longitude": "******"
}
}
}
}
还有,还有一种方法可以执行类似Flight .. * Destination的操作,而不是对路径进行硬编码?
解决方法
您可以尝试使用遮罩功能:
m=df.col_a=='Day'#boolean select
df.loc[m,'col_a']=df.loc[m,'col_b']#mask and replace
输出:
%dw 2.0
output application/json
import * from dw::util::Values
var flights =
{
"flights": {
"flight": {
"airline-name": "american","flight-code": "AA103","plane-type": null,"longitude": "33.94250107","price": "+750.00","destination": {
"airport-name": "los angeles international airport","city": "los angeles","altitude": "125","longitude":"33.94250107"
}
},"flight": {
"available-seats": "+18.00","airline-name": "delta","plane-type": "boeing 747","longitude": "33.94250107"
}
}
}
}
---
(flights mask "longitude" with "****") mask "plane-type" with "****"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。