JSON 数组迭代 - Mulesoft 4 中的树遍历
Posted
技术标签:
【中文标题】JSON 数组迭代 - Mulesoft 4 中的树遍历【英文标题】:JSON Array Iteration - Tree Traversing in Mulesoft 4 【发布时间】:2022-01-23 17:30:30 【问题描述】:输入 JSON:
"id": "/",
"code": "/",
"typeCode": "CPC",
"timeStamp": "2021-11-16T17:00:00-06:00",
"childList": [
"id": "577-1-1",
"code": "1111",
"name": "Electronics-1-1",
"displayName": "Electronics-1-1",
"active": true,
"languageCode": "en",
"typeCode": "CPC",
"childList": [
"id": "579-2-1",
"code": "2222",
"name": "Calculators-2-1",
"displayName": "Calculators-2-1",
"active": true,
"languageCode": "en",
"typeCode": "CPC",
"childList": []
,
"id": "509-2-2",
"code": "3333",
"name": "Oven-2-2",
"displayName": "Oven-2-2",
"active": true,
"languageCode": "en",
"typeCode": "CPC",
"childList": [
"id": "749-3-1",
"code": "4444",
"name": "MicroOven-3-1",
"displayName": "MicroOven-3-1",
"active": true,
"languageCode": "en",
"typeCode": "CPC",
"childList": []
]
]
,
"id": "549-1-2",
"code": "5555",
"name": "Toys, Crafts & Leisure",
"displayName": "Toys, Crafts & Leisure",
"active": true,
"languageCode": "en",
"typeCode": "CPC",
"childList": [
"id": "49674",
"code": "7605",
"name": "Pet Supplies",
"displayName": "Pet Supplies",
"active": true,
"languageCode": "en",
"typeCode": "CPC",
"childList": [
"id": "49684",
"code": "7615",
"name": "Beds & Houses",
"displayName": "Beds & Houses",
"active": true,
"languageCode": "en",
"typeCode": "CPC",
"childList": []
]
]
,
"id": "58782",
"code": "0100",
"name": "Fashion (new)",
"displayName": "Fashion",
"active": true,
"languageCode": "en",
"typeCode": "CPC",
"childList": [
"id": "49056",
"code": "6958",
"name": "Blazers & Jackets, Etc. - Department",
"displayName": "Blazers & Jackets, Etc.",
"active": true,
"languageCode": "en",
"typeCode": "CPC",
"childList": [
"id": "50851",
"code": "7837",
"name": "Dusters PDM Name",
"displayName": "Dusters PDM Display Name",
"active": true,
"languageCode": "en",
"typeCode": "CPC",
"childList": []
]
]
]
-
预期的 XML 如下 - 此处的 categoryPath 必须注明 childElement 的完整地址,并且必须分组在第一个父 childList 下
我们不会知道任何数组的确切子元素,即一个子列表中可能有“n”个子列表
第一个 childlist 数组中有多少元素 - 必须在 xml 中形成这些 CategoryList 元素
注意:这里我只是以 2 个元素为例 - 但任何 childlist 数组都可以包含任意数量的 childlists 数组。
XML 转换:
<Category CategoryId="$childList.code" CategoryPath="$childList.code/$childList.childList.code" Description="$childList.displayName" ShortDescription="$childList.name">
<Extn ExtnSyncTS="$headers.timeStamp"/>
</Category>
预期的 XML:
<CategoryListArray>
<CategoryList>
<Category CategoryId="1111" CategoryPath="1111" Description="Electronics-1-1" ShortDescription="Electronics-1-1">
<Extn ExtnSyncTS="2021-11-16T17:00:00-06:00"/>
</Category>
<Category CategoryId="2222" CategoryPath="1111/2222" Description="Calculators-2-1" ShortDescription="Calculators-2-1">
<Extn ExtnSyncTS=" "/>
</Category>
<Category CategoryId="3333" CategoryPath="1111/3333" Description="Oven-2-2" ShortDescription="Oven-2-2">
<Extn ExtnSyncTS="2021-11-16T17:00:00-06:00"/>
</Category>
<Category CategoryId="4444" CategoryPath="1111/3333/4444" Description="MicroOven-3-1" ShortDescription="MicroOven-3-1">
<Extn ExtnSyncTS="2021-11-16T17:00:00-06:00"/>
</Category>
</CategoryList>
<CategoryList>
<Category CategoryId="5555" CategoryPath="5555" Description="Toys, Crafts & Leisure" ShortDescription="Toys, Crafts & Leisure">
<Extn ExtnSyncTS="2021-11-16T17:00:00-06:00"/>
</Category>
<Category CategoryId="7605" CategoryPath="5555/7605" Description="Pet Supplies" ShortDescription="Pet Supplies">
<Extn ExtnSyncTS="2021-11-16T17:00:00-06:00"/>
</Category>
<Category CategoryId="7615" CategoryPath="5555/7605/7615" Description="Beds & Houses" ShortDescription="Beds & Houses">
<Extn ExtnSyncTS="2021-11-16T17:00:00-06:00"/>
</Category>
</CategoryList>
<CategoryList>
<Category CategoryId="0100" CategoryPath="0100" Description="Fashion" ShortDescription="Fashion (new)">
<Extn ExtnSyncTS="2021-11-16T17:00:00-06:00"/>
</Category>
<Category CategoryId="6958" CategoryPath="0100/6958" Description=""Blazers & Jackets, Etc." ShortDescription="Blazers & Jackets, Etc. - Department">
<Extn ExtnSyncTS="2021-11-16T17:00:00-06:00"/>
</Category>
<Category CategoryId="7837" CategoryPath="0100/6958/7837" Description="Dusters PDM Display Name" ShortDescription="Dusters PDM Name">
<Extn ExtnSyncTS="2021-11-16T17:00:00-06:00"/>
</Category>
</CategoryList>
</CategoryListArray>
【问题讨论】:
将每个 Category 分组到 CategoryList 元素的标准是什么? 基于 1st parent childList.. 如果输入 json 有 20 个 1st parent childlist,则 mulesoft 必须发送 20 条消息.. 1st parent 中的所有其他 childlists 必须分组到 categorylist 【参考方案1】:我修改了之前的答案以支持新的输出。我使用了一个嵌套函数来重用之前未触及的方法,同时在顶部添加更改,因此脚本中只有一个函数可见。
%dw 2.0
output application/xml
fun mapToCategoriesArrayList(data) = do
fun mapChildren(x, timestamp, path)=
x match
case o is Object ->
([
Category @(CategoryId: o.id, CategoryPath: (if (isEmpty(path)) "" else path ++ "/") ++ o.code, Description: o.name, ShortDescription: o.displayName ): Extn: timestamp
]
++ mapChildren(o.childList, timestamp, (if (isEmpty(path)) "" else path ++ "/") ++ o.code))
case a is Array ->
(a flatMap mapChildren($, timestamp, path) )
else -> $
---
CategoryListArray:
(
data.childList map
CategoryList:
mapChildren($, data.timeStamp, "")
reduce ((item, accumulator=) -> accumulator ++ item )
) reduce ((item, accumulator=) -> accumulator ++ item )
---
mapToCategoriesArrayList(payload)
输出:
<?xml version='1.0' encoding='UTF-8'?>
<CategoryListArray>
<CategoryList>
<Category CategoryId="577-1-1" CategoryPath="1111" Description="Electronics-1-1" ShortDescription="Electronics-1-1">
<Extn>2021-11-16T17:00:00-06:00</Extn>
</Category>
<Category CategoryId="579-2-1" CategoryPath="1111/2222" Description="Calculators-2-1" ShortDescription="Calculators-2-1">
<Extn>2021-11-16T17:00:00-06:00</Extn>
</Category>
<Category CategoryId="509-2-2" CategoryPath="1111/3333" Description="Oven-2-2" ShortDescription="Oven-2-2">
<Extn>2021-11-16T17:00:00-06:00</Extn>
</Category>
<Category CategoryId="749-3-1" CategoryPath="1111/3333/4444" Description="MicroOven-3-1" ShortDescription="MicroOven-3-1">
<Extn>2021-11-16T17:00:00-06:00</Extn>
</Category>
</CategoryList>
<CategoryList>
<Category CategoryId="549-1-2" CategoryPath="5555" Description="Toys, Crafts & Leisure" ShortDescription="Toys, Crafts & Leisure">
<Extn>2021-11-16T17:00:00-06:00</Extn>
</Category>
<Category CategoryId="49674" CategoryPath="5555/7605" Description="Pet Supplies" ShortDescription="Pet Supplies">
<Extn>2021-11-16T17:00:00-06:00</Extn>
</Category>
<Category CategoryId="49684" CategoryPath="5555/7605/7615" Description="Beds & Houses" ShortDescription="Beds & Houses">
<Extn>2021-11-16T17:00:00-06:00</Extn>
</Category>
</CategoryList>
<CategoryList>
<Category CategoryId="58782" CategoryPath="0100" Description="Fashion (new)" ShortDescription="Fashion">
<Extn>2021-11-16T17:00:00-06:00</Extn>
</Category>
<Category CategoryId="49056" CategoryPath="0100/6958" Description="Blazers & Jackets, Etc. - Department" ShortDescription="Blazers & Jackets, Etc.">
<Extn>2021-11-16T17:00:00-06:00</Extn>
</Category>
<Category CategoryId="50851" CategoryPath="0100/6958/7837" Description="Dusters PDM Name" ShortDescription="Dusters PDM Display Name">
<Extn>2021-11-16T17:00:00-06:00</Extn>
</Category>
</CategoryList>
</CategoryListArray>
更新:修复 CategoryList 分组
【讨论】:
从上面的 dw 代码也可以看出,输出结构与之前的相同。我当前的 o/p 结构已更改。我需要一个 categoryList 下 json 中第一个父子项的所有类别。但我在括号中做了一些更改,它正在工作 - 谢谢 CategoryList的分组出错了。我更新了答案以修复它。以上是关于JSON 数组迭代 - Mulesoft 4 中的树遍历的主要内容,如果未能解决你的问题,请参考以下文章