如何根据过滤条件后的key创建数组?

问题描述 投票:0回答:1

我有一个如下所示的数组,它是过滤条件的输出。它具有像 documentItemName1、documentItemName2、documentItemName3 这样的字段,最小值为 3,最大值为 3。

每个文档都有类型和子类型。我将使用此数组根据内容版本进行过滤,并根据不存在的 documentItemName1 创建对象。

有效负载:

 [
      {
        "contentVersion": "068DQ000000lK0iYAE",
        "documentName": "ESRA with Exhibit_A",
        "documentItemName1": "Signature001",
        "typeCd1": "Signature",
        "typeCd2": null,
        "typeCd3": null,
        "subtypeCd1": "CAPTURE",
        "subtypeCd2": null,
        "subtypeCd3": null
      },
      {
        "contentVersion": "068DQ000000kK0OYAU",
        "documentName": "Agreement 1",
        "documentItemName1": "Signature001",
        "documentItemName2": "Signature002",
        "typeCd1": "Signature",
        "typeCd2": "Signature",
        "typeCd3": null,
        "subtypeCd1": "CAPTURE",
        "subtypeCd2": "LABEL",
        "subtypeCd3": null
      },
      {
        "contentVersion": "068DQ000000lK0OYAU",
        "documentName": "Agreement 2",
        "documentItemName1": "Signature001",
        "documentItemName2": "Signature002",
        "documentItemName3": "Signature003",
        "typeCd1": "Signature",
        "typeCd2": "Signature",
        "typeCd3": "Signature",
        "subtypeCd1": "CAPTURE",
        "subtypeCd2": "CAPTURE",
        "subtypeCd3": "CAPTURE"
      }
    ]

我尝试过下面的脚本,但不确定如何根据其中的 documentItemName{no} 来创建对象。

例如:从有效负载中,我过滤到此 contentVersion =“068DQ000000lK0OYAU”,其中包含 documentItemName1、documentItemName2、documentItemName3。因此,documentItemToSigner 数组应该有 3 个像这样映射的对象

"documentItemToSigner": [{

documentItemName: documentItemName1,
"typeCd": typeCd1,
"subtypeCd": subtypeCd1
},
 {

documentItemName: documentItemName2,
"typeCd": typeCd2,
"subtypeCd": subtypeCd3
},
 {

documentItemName: documentItemName3,
"typeCd": typeCd3,
 "subtypeCd": subtypeCd3
}]

对于 ex2:从有效负载中,如果我过滤到此 contentVersion = "068DQ000000lK0iYAE",其中只有 documentItemName1。因此,documentItemToSigner 数组应该有 1 个像这样映射的对象

"documentItemToSigner": [{
        
        documentItemName: documentItemName1,
        "typeCd": typeCd1,
        "subtypeCd": subtypeCd1
        }]
    
"documentItemToSigner": (payload filter ((g, index) -> g.contentVersion == "068DQ000000lK0OYAU")) map ((item, index) -> {
    "typeCd": item."typeCd{n}",
    "subtypeCd": item."subtypeCd{n}",
    "eSignLiveExtension": {
      "extractInd": true
    },
    "documentItemName": item."documentItemName{n}"
  })

预期输出:

{
  "documentItemToSigner": [
    {
      "typeCd": "Signature",
      "subtypeCd": "CAPTURE",
      "eSignLiveExtension": {
        "extractInd": true
      },
      "documentItemName": "Signature001"
    },
    {
      "typeCd": "Signature",
      "subtypeCd": "LABEL",
      "documentItemName": "Signature002",
      "eSignLiveExtension": {
        "extractInd": false
      }
    },
 {
      "typeCd": "Signature",
      "subtypeCd": "CAPTURE",
      "eSignLiveExtension": {
        "extractInd": true
      },
      "documentItemName": "Signature003"
    }
  ]
}
mule dataweave mule4
1个回答
0
投票

这个怎么样?

%dw 2.0
output application/json
var contentVersionVar = "068DQ000000lK0iYAE"
---
{
    documentItemToSigner: (payload map (item, index) -> {
        typeCd: item."typeCd$(index+1)",
        subtypeCd: item."subtypeCd$(index+1)",
        eSignLiveExtension: {
            extractInd: true // where does this come from?
        },
        documentItemName: item."documentItemName$(index+1)",
        TEMP: item.contentVersion
    }) filter ($.TEMP == contentVersionVar) map ($ - "TEMP")
}
© www.soinside.com 2019 - 2024. All rights reserved.