问题描述
我正在尝试选择性地更新嵌套层次数据结构中的某些值。
我有嵌套的输入数据,例如
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 "****"