Mule DWL-展平json分层元素
我的json有效负载如下所示:Mule DWL-展平json分层元素,mule,dataweave,mule-component,Mule,Dataweave,Mule Component,我的json有效负载如下所示: Attributes:{ Contact:{ Address:{ PersonName:"John", Line1:"sdsds", Line2:"sdsdsd", SubAddress:{ PersonName:"John", Line1:"ggghg",
Attributes:{
Contact:{
Address:{
PersonName:"John",
Line1:"sdsds",
Line2:"sdsdsd",
SubAddress:{
PersonName:"John",
Line1:"ggghg",
Line2:"xzxzxzx"
}
},
Phone:{
Home:"2323232323",
Office:"4545454545"
}
},
Id:"2343434",
order:"3343434"
}
我想将层次结构展平到低于输出
Attributes:{
Contact.Address.PersonName:"John",
Contact.Address.Line1:"sdsds",
Contact.Address.Line2:"sdsdsd",
Contact.Address.SubAddress.PersonName:"John",
Contact.Address.SubAddress.Line1:"ggghg",
Contact.Address.SubAddress.Line2:"xzxzxzx",
Contact.Phone.Home:"2323232323",
Contact.Phone.Office:"4545454545",
Id:"2343434",
order:"3343434"
}
属性元素可以有任意数量的复杂元素,如“地址”和“联系人”。在编码时,我们不知道复杂元素的键值。DWL应该能够产生单级输出。想要在Mule 3中使用dw1实现此扁平化的通用解决方案。请提供帮助。下面的脚本递归地遍历负载并构造单键值 配对对象
%dw 1.0
%output application/json
%var payload = {"Attributes":{"Contact":{"Address":{"SubAddress":[{"PersonName1":"John","Line1":"ggghg","Line2":"xzxzxzx"},{"PersonName":"Jar","Line1":"ggghg","Line2":"xzxzxzx"}]},"Phone":{"Home":"2323232323","Office":"4545454545"}},"Id":"2343434","order":"3343434"}}
%function constructKeys(oldKeys, newKey) oldKeys ++ '.' ++ newKey
%function writeData(json ,output, keys) json match {
case is :null -> null,
case is :array -> {(json map ((item, index) -> writeData(item, output, keys ++ index)))},
case is :object -> {((json pluck $$) map writeData(json[$],output,constructKeys(keys,$)))},
default -> output ++ {(keys[1 to -1]): json }
}
---
Attributes: writeData(payload.Attributes,{}, '')
下面的脚本递归地遍历负载并构造单键值 配对对象
%dw 1.0
%output application/json
%var payload = {"Attributes":{"Contact":{"Address":{"SubAddress":[{"PersonName1":"John","Line1":"ggghg","Line2":"xzxzxzx"},{"PersonName":"Jar","Line1":"ggghg","Line2":"xzxzxzx"}]},"Phone":{"Home":"2323232323","Office":"4545454545"}},"Id":"2343434","order":"3343434"}}
%function constructKeys(oldKeys, newKey) oldKeys ++ '.' ++ newKey
%function writeData(json ,output, keys) json match {
case is :null -> null,
case is :array -> {(json map ((item, index) -> writeData(item, output, keys ++ index)))},
case is :object -> {((json pluck $$) map writeData(json[$],output,constructKeys(keys,$)))},
default -> output ++ {(keys[1 to -1]): json }
}
---
Attributes: writeData(payload.Attributes,{}, '')
是否有方法将数组索引作为键的一部分捕获:“Contact.Address.SubAddress.1.PersonName”:“John”、“Contact.Address.SubAddress.2.PersonName”:“John”,是否有方法将数组索引作为键的一部分捕获:“Contact.Address.SubAddress.1.PersonName”:“John”、“Contact.Address.SubAddress.2.PersonName”:“John”,