dataweave 脚本迭代嵌套的字符串文字数组
dataweave script to iterate over a nested array of string literals
所以我有以下负载:
<?xml version="1.0" encoding="utf8" ?>
<Output>
<Error>
<Status>0</Status>
<Details>No errors</Details>
</Error>
<Synopsis>
<Count>451</Count>
</Synopsis>
<BankAccounts>
<BankAccount AcctNo="103" CustName="Frank" BalanceAmount="" Inactive="N" NoOfAccounts="1" >
<Addresses>
<Address>ABC</Address>
<Address>XYZ</Address>
</Addresses>
</BankAccount>
<BankAccount AcctNo="101" CustName="Jane" BalanceAmount="10005" Inactive="N" NoOfAccounts="1" >
<Addresses>
<Address>LMN</Address>
<Address>QWE</Address>
</Addresses>
</BankAccount>
</BankAccounts>
</Output>
我需要将其转换成下面的形式 json :
[
{
"accountNumber": "103",
"customerName": "Frank",
"balanceAmount": "",
"inactive": "N",
"noOfAccounts": "1",
"address": [
"ABC","XYZ"
]
},
{
"accountNumber": "101",
"customerName": "Jane",
"balanceAmount": "10005",
"inactive": "N",
"noOfAccounts": "1",
"address": [
"LMN","QWE"
]
}
]
我使用下面的 dataweave 脚本快到了:
%dw 2.0
output application/json writeAttributes=true
---
payload.Output.BankAccounts.*BankAccount map {
accountNumber: $.@AcctNo,
customerName: $.@CustName,
balanceAmount: $.@BalanceAmount,
inactive: $.@Inactive,
noOfAccounts: $.@NoOfAccounts,
address: $.Addresses.*Address map (value,index)->{
}
}
但是我无法将 Addresses 元素(其中包含多个 Address 元素转换为字符串数组,如下所示:
"address": [
"LMN","QWE"
]
我正在为最后一部分苦苦挣扎,有什么想法吗?
您可以在不使用 {}
的情况下使用地址数组中的值。 DW在使用大括号时创建一个对象。
%dw 2.0
output application/json
---
payload.Output.BankAccounts.*BankAccount map ((account) -> {
accountNumber: account.@AcctNo,
customerName: account.@CustName,
balanceAmount: account.@BalanceAmount,
inactive: account.@Inactive,
noOfAccounts: account.@NoOfAccounts,
address: account.Addresses.*Address map ((value)-> value)
})
注意XML没有数组的概念。地址只是一个具有多个地址子元素的元素。
您可以直接使用多值选择器。它将 return 匹配键值的数组。只有当你想进一步转换每个元素时才需要映射。
$.Addresses.*Address
所以我有以下负载:
<?xml version="1.0" encoding="utf8" ?>
<Output>
<Error>
<Status>0</Status>
<Details>No errors</Details>
</Error>
<Synopsis>
<Count>451</Count>
</Synopsis>
<BankAccounts>
<BankAccount AcctNo="103" CustName="Frank" BalanceAmount="" Inactive="N" NoOfAccounts="1" >
<Addresses>
<Address>ABC</Address>
<Address>XYZ</Address>
</Addresses>
</BankAccount>
<BankAccount AcctNo="101" CustName="Jane" BalanceAmount="10005" Inactive="N" NoOfAccounts="1" >
<Addresses>
<Address>LMN</Address>
<Address>QWE</Address>
</Addresses>
</BankAccount>
</BankAccounts>
</Output>
我需要将其转换成下面的形式 json :
[
{
"accountNumber": "103",
"customerName": "Frank",
"balanceAmount": "",
"inactive": "N",
"noOfAccounts": "1",
"address": [
"ABC","XYZ"
]
},
{
"accountNumber": "101",
"customerName": "Jane",
"balanceAmount": "10005",
"inactive": "N",
"noOfAccounts": "1",
"address": [
"LMN","QWE"
]
}
]
我使用下面的 dataweave 脚本快到了:
%dw 2.0
output application/json writeAttributes=true
---
payload.Output.BankAccounts.*BankAccount map {
accountNumber: $.@AcctNo,
customerName: $.@CustName,
balanceAmount: $.@BalanceAmount,
inactive: $.@Inactive,
noOfAccounts: $.@NoOfAccounts,
address: $.Addresses.*Address map (value,index)->{
}
}
但是我无法将 Addresses 元素(其中包含多个 Address 元素转换为字符串数组,如下所示:
"address": [
"LMN","QWE"
]
我正在为最后一部分苦苦挣扎,有什么想法吗?
您可以在不使用 {}
的情况下使用地址数组中的值。 DW在使用大括号时创建一个对象。
%dw 2.0
output application/json
---
payload.Output.BankAccounts.*BankAccount map ((account) -> {
accountNumber: account.@AcctNo,
customerName: account.@CustName,
balanceAmount: account.@BalanceAmount,
inactive: account.@Inactive,
noOfAccounts: account.@NoOfAccounts,
address: account.Addresses.*Address map ((value)-> value)
})
注意XML没有数组的概念。地址只是一个具有多个地址子元素的元素。
您可以直接使用多值选择器。它将 return 匹配键值的数组。只有当你想进一步转换每个元素时才需要映射。
$.Addresses.*Address