修改现有的Json密钥:Mule
我的意见修改现有的Json密钥:Mule,mule,dataweave,Mule,Dataweave,我的意见 { "Root": { "order": [ { "locale": "en-US", "orderItems": [ { "product": { "partNumber": "23853864" }, "itemSpecifics": { "options": {
{
"Root": {
"order": [
{
"locale": "en-US",
"orderItems": [
{
"product": {
"partNumber": "23853864"
},
"itemSpecifics": {
"options": {
"color": "Olive",
"size": "S"
},
"actualPrice": "7",
"customItemData": {
"TEMP_8401": "8.95",
"TEMP_150207": "3.00"
}
}
}
]
}
... Large amount of JSON Data ...
]
}
}
预期产量
{
"Root": {
"order": [
{
"locale": "en-US",
"orderItems": [
{
"product": {
"partNumber": "23853864"
},
"itemSpecifics": {
"options": {
"color": "Olive",
"size": "S"
},
"actualPrice": "7",
"customItemData": {
"8401": "8.95",
"150207": "3.00"
}
}
}
]
}
... Large amount of JSON Data ...
]
}
}
我想删除
“customItemData”
对象键中的“TEMP”
,但我不想手动重新映射整个JSON对象,逐个分配属性。还有别的选择吗?DataWeave中有没有更短的逻辑?我正在使用Mule 3.9.0。这使用递归和模式匹配遍历数据结构并修改键。如果键不包含字符串“TEMP”
,它会保持原样,如果包含,它会根据您的要求对其进行修改
%dw 1.0
%output application/json
%function applyToKeys(e, fn)
e match {
:array -> $ map ((v) -> applyToKeys(v, fn)),
:object -> $ mapObject ((v, k) -> {(fn(k)): applyToKeys(v, fn)}),
default -> $
}
---
applyToKeys(payload,
((key) -> ((key as :string) splitBy "_" )[1]
when ((key as :string) contains "TEMP")
otherwise key))
当使用这样的解决方案时,请记住权衡。代码当然不那么冗长,但它需要对递归、模式匹配、lambdas和高阶函数等高级概念有深入的理解。感谢@jerney提供的解决方案。。。但是我在执行::default->$^时遇到异常,没有名为“:default”的类型。Abhilash,如果有机会,请重试上述代码。当然没有名为
:default
的类型正确的关键字是default
。对任何混乱表示歉意。希望这有帮助!哇…很有魅力…谢谢:D