如何使用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 "****"

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...