需要一种优化的方式来获得所需的输出
Need an optimized way to get the required output
是否有优化的方法trim下面数据数组字段值的开始和结束空格,使用了三种方法,但需要更优化的方法。
注意:数据数组中可能有超过20个对象,每个对象有超过50个字段。以下有效载荷只是一个示例;字段值可以包含任意大小的数字或字符串或日期。
{
"School": "XYZ High school",
"data": [
{
"student Name": "XYZ ",
"dateofAdmission": "2021-06-09 ",
"percentage": "89 "
},
{
"student Name": "ABC ",
"dateofAdmission": "2021-08-04 ",
"percentage": "90 "
},
{
"student Name": "PQR ",
"dateofAdmission": "2021-10-01 ",
"percentage": "88 "
}
]
}
所需输出:
{
"School": "XYZ High school",
"data": [
{
"student Name": "XYZ",
"dateofAdmission": "2021-06-09",
"percentage": "89"
},
{
"student Name": "ABC",
"dateofAdmission": "2021-08-04",
"percentage": "90"
},
{
"student Name": "PQR",
"dateofAdmission": "2021-10-01",
"percentage": "88"
}
]
}
我使用过的三种方法:
第一种方法:
%dw 2.0
output application/json
//variable to remove beginning and end blank spaces from values in key:value pairs for data
var payload1 = payload.data map ((value , key ) ->
value mapObject ( ($$ )) : trim($))
---
//constructed the payload
payload - "data" ++ data: payload1 map ((item, index) -> {
(item)
})
第二种方法:
%dw 2.0
output application/json
---
payload - "data" ++ data: payload.data map ((value , key ) ->
value mapObject ( ($$ )) : trim($)) map ((item, index) -> {
(item)
})
第三种方法:
%dw 2.0
output application/json
---
{
"Name":payload.School,
"data": payload.data map ( $ mapObject (($$):trim($) ) )
}
另一种使用更新运算符的解决方案:
%dw 2.0
output application/json
---
payload update {
case data at .data ->
data map ($ mapObject ((value, key, index) -> (key):trim(value) ))
}
请注意,您的前两个解决方案完全相同,并且都在末尾有一个不需要的 map() 似乎没有任何用途。第三个解决方案非常相似,但是学校名称使用了不正确的键名称(Name
而不是示例中的 School
)。除了这些小问题外,每个解决方案都没有特别的问题。
是否有优化的方法trim下面数据数组字段值的开始和结束空格,使用了三种方法,但需要更优化的方法。
注意:数据数组中可能有超过20个对象,每个对象有超过50个字段。以下有效载荷只是一个示例;字段值可以包含任意大小的数字或字符串或日期。
{
"School": "XYZ High school",
"data": [
{
"student Name": "XYZ ",
"dateofAdmission": "2021-06-09 ",
"percentage": "89 "
},
{
"student Name": "ABC ",
"dateofAdmission": "2021-08-04 ",
"percentage": "90 "
},
{
"student Name": "PQR ",
"dateofAdmission": "2021-10-01 ",
"percentage": "88 "
}
]
}
所需输出:
{
"School": "XYZ High school",
"data": [
{
"student Name": "XYZ",
"dateofAdmission": "2021-06-09",
"percentage": "89"
},
{
"student Name": "ABC",
"dateofAdmission": "2021-08-04",
"percentage": "90"
},
{
"student Name": "PQR",
"dateofAdmission": "2021-10-01",
"percentage": "88"
}
]
}
我使用过的三种方法:
第一种方法:
%dw 2.0
output application/json
//variable to remove beginning and end blank spaces from values in key:value pairs for data
var payload1 = payload.data map ((value , key ) ->
value mapObject ( ($$ )) : trim($))
---
//constructed the payload
payload - "data" ++ data: payload1 map ((item, index) -> {
(item)
})
第二种方法:
%dw 2.0
output application/json
---
payload - "data" ++ data: payload.data map ((value , key ) ->
value mapObject ( ($$ )) : trim($)) map ((item, index) -> {
(item)
})
第三种方法:
%dw 2.0
output application/json
---
{
"Name":payload.School,
"data": payload.data map ( $ mapObject (($$):trim($) ) )
}
另一种使用更新运算符的解决方案:
%dw 2.0
output application/json
---
payload update {
case data at .data ->
data map ($ mapObject ((value, key, index) -> (key):trim(value) ))
}
请注意,您的前两个解决方案完全相同,并且都在末尾有一个不需要的 map() 似乎没有任何用途。第三个解决方案非常相似,但是学校名称使用了不正确的键名称(Name
而不是示例中的 School
)。除了这些小问题外,每个解决方案都没有特别的问题。