问题描述
%dw 2.0 output application/json var attributeIdMapping =
${vault::attributeIdMapping}
--- {
"objectTypeId": 3,"attributes": vars.requestBody mapObject (value,key) ->
{
"objectTypeAttributeId": (attributeIdMapping.attributes filter ($.name == key))[0].id,"objectAttributeValues": [{
"value": value,"key": key
}]
} }
attributeIdMapping:
{
"attributes": [
{
"name": "accountType","id": "87"
},{
"name": "accountClass","id": "89"
},{
"name": "accountName","id": "85"
},{
"name": "displayName","id": "18"
},{
"name": "accountCategory","id": "88"
},{
"name": "accountNumber","id": "84"
},{
"name": "description","id": "86"
},{
"name": "accountGroup","id": "90"
}
]
}
vars.requestBody:
{
"displayName": "TestMulesoft2","description": "Test"
}
但是我的过滤器最后显示null。据我了解,密钥没有传递到地图本身下面的级别。我该如何工作?
解决方法
我将所有输入数据放入脚本中以简化复制。问题似乎是使用运算符==
比较字符串,并且键类型返回空。使用似乎attempts to coerce的运算符~=
返回预期结果:
%dw 2.0
output application/json
var attributeIdMapping =
{
"attributes": [
{
"name": "accountType","id": "87"
},{
"name": "accountClass","id": "89"
},{
"name": "accountName","id": "85"
},{
"name": "displayName","id": "18"
},{
"name": "accountCategory","id": "88"
},{
"name": "accountNumber","id": "84"
},{
"name": "description","id": "86"
},{
"name": "accountGroup","id": "90"
}
]
}
var requestBody = {
"displayName": "TestMulesoft2","description": "Test"
}
--- {
"objectTypeId": 3,"attributes": requestBody mapObject (value,key) ->
{
"objectTypeAttributeId": (attributeIdMapping.attributes filter ($.name ~= key))[0].id,"objectAttributeValues": [{
"value": value,"key": key
}]
} }
输出:
{
"objectTypeId": 3,"attributes": {
"objectTypeAttributeId": "18","objectAttributeValues": [
{
"value": "TestMulesoft2","key": "displayName"
}
],"objectTypeAttributeId": "86","objectAttributeValues": [
{
"value": "Test","key": "description"
}
]
}
}