如何使用 dataweave 在 reduce 函数中增加计数器

How to increment a counter inside a reduce function using dataweave

我正在尝试使用数据编织(用于 EDI 811 映射)进行 JSON 到 JSON 的转换。我的要求是对于 HL 循环内的每个 VEH 段(具有车辆信息),我想启动一个从 1 到车辆记录数的计数器。

My input format is -

[
  {
    "IDENTIFIER": "ABC",
    "HOST_ID": "XYZ",
    "BusinessKey": "11111",
    "HL0": {
      "HL0_STATE_ID": "22",
      "HL0_RPT_SYS_ID": "ABCD",
      "HL0_CO_NAIC": "11111"
    },
    "HL1": {
      "HL1_NAIC_COMP_CODE": "111111",
      "HL1_CO_ADDRESS": "AAAA BBBB CCCCC",
      "HL1_CO_CITY": "MABCD",
      "HL1_CO_STATE": "AB",
      "HL1_CO_ZIP_9": "12345"
    },
    "Details": [
       {
        "HL4": {
          "HL4_POLICY_NUM": "8767886857",
          "HL4_TRAN_CODE_ALPHA": "JGJ",
          "HL4_CYCLE_DATE_CC": "19",
          "HL4_CYCLE_DATE_YYMMDD": "200511",
          "HL4_STATE_CODE": "VV",
          "HL4_STATE_REPORTING_ID": "0000",
          "HL4_PRODUCT_CODE": "DD",
          "HL4_SUB_PRODUCT_CODE": "",
          "HL4_SENDER_ID": "",
          "HL4_POLICY_TYPE": "V",
          "HL4_PROTEUS_TRANSACTION": "00000",
          "HL4_TRAN_SUB_TYPE_CODE": "",
          "HL4_TRAN_DATE_CC": "00",
          "HL4_TRAN_DATE_YYMMDD": "000000",
          "HL4_TRAN_TIME": "00000000",
          "HL4_POL_EFF_DATE_CC": "00",
          "HL4_POL_EFF_DATE_YYMMDD": "000000",
          "HL4_POL_EXP_DATE_CC": "00",
          "HL4_POL_EXP_DATE_YYMMDD": "000000",
          "HL4_PERSON_ORG_INDIC": "1",
          "HL4_LIAB_INDIC": "",
          "HL4_AGENT_CODE": {
            "HL4_AGENT_PREFIX": "",
            "HL4_PRODUCER_AGENT": "00000"
          },
          "HL4_CANCEL_EFF_DATE": {
            "HL4_CANCEL_EFF_DATE_CC": "00",
            "HL4_XCL_EFF_DATE_YYMMDD": "000000"
          },
          "HL4_DRV_NAME": {
            "HL4_DRV_LAST_NAME": "DDDDDD",
            "HL4_DRV_FIRST_NAME_MI": "PPPPP"
          },
          "HL4_DRV_ADDRESS": "KHHKHK HJHKHKH GGGGGG RD S",
          "HL4_DRV_CITY": "IOHKHL",
          "HL4_DRV_STATE": "MB",
          "HL4_DRV_ZIP": "68686",
          "HL4_DRV_DLN": "875758865",
          "HL4_DRV_DLN_STATE": "BJ",
          "HL4_DRV_DOB_NUM": {
            "HL4_DRV_DOB_YYMMDD": "20",
            "HL4_DRV_DOB_CC": "200525"
          },
          "HL4_DRV_SEX": "",
          "HL4_VEH_EFF_DATE_NUM": {
            "HL4_VEH_EFF_DATE_CC": "00",
            "HL4_VEH_EFF_DATE_YYMMDD": "000000"
          },
          "HL4_VEH_EXP_DATE_NUM": {
            "HL4_VEH_EXP_DATE_CC": "20",
            "HL4_VEH_EXP_DATE_YYMMDD": "220423"
          }
        },
        "HL5": [
          {
            "HL5_VEH_VIN": "1BJHFJBKHJK6876868",
            "HL5_VEH_MAKE": "CDRJV",
            "HL5_VEH_CC": "19",
            "HL5_VEH_YY": "99"
          },
          {
            "HL5_VEH_VIN": "1BVNVJH68686JHG979",
            "HL5_VEH_MAKE": "JHGJGJG",
            "HL5_VEH_CC": "19",
            "HL5_VEH_YY": "92"
          }
        ]
      },
      {
        "HL4": {
          "HL4_POLICY_NUM": "6877578787",
          "HL4_TRAN_CODE_ALPHA": "ABC",
          "HL4_CYCLE_DATE_CC": "20",
          "HL4_CYCLE_DATE_YYMMDD": "210805",
          "HL4_STATE_CODE": "30",
          "HL4_STATE_REPORTING_ID": "0000",
          "HL4_PRODUCT_CODE": "CA",
          "HL4_SUB_PRODUCT_CODE": "",
          "HL4_SENDER_ID": "",
          "HL4_POLICY_TYPE": "V",
          "HL4_PROTEUS_TRANSACTION": "00000",
          "HL4_TRAN_SUB_TYPE_CODE": "",
          "HL4_TRAN_DATE_CC": "00",
          "HL4_TRAN_DATE_YYMMDD": "000000",
          "HL4_TRAN_TIME": "00000000",
          "HL4_POL_EFF_DATE_CC": "00",
          "HL4_POL_EFF_DATE_YYMMDD": "000000",
          "HL4_POL_EXP_DATE_CC": "00",
          "HL4_POL_EXP_DATE_YYMMDD": "000000",
          "HL4_PERSON_ORG_INDIC": "2",
          "HL4_LIAB_INDIC": "",
          "HL4_AGENT_CODE": {
            "HL4_AGENT_PREFIX": "",
            "HL4_PRODUCER_AGENT": "00000"
          },
          "HL4_CANCEL_EFF_DATE": {
            "HL4_CANCEL_EFF_DATE_CC": "00",
            "HL4_XCL_EFF_DATE_YYMMDD": "000000"
          },
          "HL4_DRV_NAME": {
            "HL4_DRV_LAST_NAME": "EE SSSSSS LLC",
            "HL4_DRV_FIRST_NAME_MI": ""
          },
          "HL4_DRV_ADDRESS": "6869800 CCCCCC DR",
          "HL4_DRV_CITY": "JFJJFJG",
          "HL4_DRV_STATE": "GK",
          "HL4_DRV_ZIP": "868875",
          "HL4_DRV_DLN": "77797968123",
          "HL4_DRV_DLN_STATE": "NM",
          "HL4_DRV_DOB_NUM": {
            "HL4_DRV_DOB_YYMMDD": "20",
            "HL4_DRV_DOB_CC": "210907"
          },
          "HL4_DRV_SEX": "",
          "HL4_VEH_EFF_DATE_NUM": {
            "HL4_VEH_EFF_DATE_CC": "20",
            "HL4_VEH_EFF_DATE_YYMMDD": "220410"
          },
          "HL4_VEH_EXP_DATE_NUM": {
            "HL4_VEH_EXP_DATE_CC": "00",
            "HL4_VEH_EXP_DATE_YYMMDD": "000000"
          }
        },
        "HL5": [
          {
            "HL5_VEH_VIN": "1GJHHKHGJGJGJG878",
            "HL5_VEH_MAKE": "JGKJGJH",
            "HL5_VEH_CC": "20",
            "HL5_VEH_YY": "15"
          },
          {
            "HL5_VEH_VIN": "1JHGJGGKG97968687",
            "HL5_VEH_MAKE": "GJJG",
            "HL5_VEH_CC": "20",
            "HL5_VEH_YY": "22"
          },
          {
            "HL5_VEH_VIN": "186876BJHGJGJ7868",
            "HL5_VEH_MAKE": "GJGKHKH",
            "HL5_VEH_CC": "20",
            "HL5_VEH_YY": "20"
          }
        ]
      }
    ]
  }
]

我的输出格式JSON如下,VEH段全部映射自上述JSON输入中的HL5记录。在 VEH01 中,我想映射 HL5 记录的 index/occurrence,所以如果 HL4 有 3 个 HL5 记录,我们将得到 HL-VEH 循环 3 次,并且 VEH01 应该从'1'开始递增第一次出现, '2' 表示第二次出现,'3' 表示第三次出现,对于下一个 HL4-HL5 记录,VEH01 应该再次从“1”-

开始

{
  "TransactionSets": {
    "v003050": {
      "811": [
        {
          "Group": {
            "GS04": "..."
          },
          "Heading": {
            "020_BIG": {
              "BIG01": "...",
              "BIG02": ".."
            },
....
....
        "Detail": {
            "010_HL_Loop": [
              {
                "010_HL": {
                  "HL01": "1",
                  "HL03": "1",
                  "HL04": "1"
                },
...
...
             {
                "010_HL": {
                  "HL01": "8",
                  "HL02": "7",
                  "HL03": "5",
                  "HL04": "0"
                },
                "020_LX_Loop": [
                  {
                    "020_LX": {
                      "LX01": 1
                    },
                    "025_VEH": {
                      "VEH01": 1,             ---> This should be 1
                      "VEH02": "1BJHFJBKHJK6876868",
                      "VEH03": ..,
                      "VEH04": ..,
                      "VEH05": "..",
                      "VEH06": "CDRJV"
                    }
                  }
                ]
              },
              {
                "010_HL": {
                  "HL01": "9",
                  "HL02": "7",
                  "HL03": "5",
                  "HL04": "0"
                },
                "020_LX_Loop": [
                  {
                    "020_LX": {
                      "LX01": 1
                    },
                    "025_VEH": {
                      "VEH01": 1,             ---> This should be 2
                      "VEH02": "1BVNVJH68686JHG979",
                      "VEH03": ..,
                      "VEH04": ..,
                      "VEH05": "..",
                      "VEH06": "JHGJGJG"
                    }
                  }
                ]
              },
...
...
...
                "010_HL": {
                  "HL01": "11",
                  "HL02": "10",
                  "HL03": "5",
                  "HL04": "0"
                },
                "020_LX_Loop": [
                  {
                    "020_LX": {
                      "LX01": 1
                    },
                    "025_VEH": {
                      "VEH01": 1,             ---> This should be 1
                      "VEH02": "1GJHHKHGJGJGJG878",
                      "VEH03": ..,
                      "VEH04": ..,
                      "VEH05": "..",
                      "VEH06": "JGKJGJH"
                    }
                  }
                ]
              },
              {
                "010_HL": {
                  "HL01": "12",
                  "HL02": "10",
                  "HL03": "5",
                  "HL04": "0"
                },
                "020_LX_Loop": [
                  {
                    "020_LX": {
                      "LX01": 1
                    },
                    "025_VEH": {
                      "VEH01": 1,             ---> This should be 2
                      "VEH02": "1JHGJGGKG97968687",
                      "VEH03": ..,
                      "VEH04": ..,
                      "VEH05": "..",
                      "VEH06": "GJJG"
                    }
                  }
                ]
              },
              {
                "010_HL": {
                  "HL01": "13",
                  "HL02": "10",
                  "HL03": "5",
                  "HL04": "0"
                },
                "020_LX_Loop": [
                  {
                    "020_LX": {
                      "LX01": 1
                    },
                    "025_VEH": {
                      "VEH01": 1,             ---> This should be 3
                      "VEH02": "186876BJHGJGJ7868",
                      "VEH03": ..,
                      "VEH04": .,
                      "VEH05": "..",
                      "VEH06": "GJGKHKH"
                    }
                  }
                ]
              }
            ]

我在dataweave中的映射如下-

%dw 2.0
import * from dw::core::Strings
output application/json

fun toHL1(rec, HL1Hierarchy) = {
    "010_HL": {
        HL01: (HL1Hierarchy + 1) as String,
        HL03: "1",
        HL04: "1"
    },
    "110_NM1_Loop": {
        "110_NM1": {
            NM101: "..",
            NM102: "..",
            NM103: "...",
            NM108: "..",
            NM109: rec.HL1.HL1_NAIC_COMP_CODE,
        }
    },
    "210_IT1_Loop": [{
        "210_IT1": {
            IT102: 1,
            IT103: "..",
            IT104: 0
        },
        "270_DTM": [{
            DTM01: "368",
            DTM02: rec.Details.HL4.HL4_CYCLE_DATE_YYMMDD[0] as Date {
                format: "yyMMdd"
            } default "000000",
            DTM05: rec.Details.HL4.HL4_CYCLE_DATE_CC[0] as Number
        }]
    }]
}
//////********************** */
fun toOrder(rec, shipHierarchy) = {
    "010_HL": {
        HL01: (shipHierarchy + 1) as String,
        HL02: "1",
        HL03: "2",
        HL04: "1"
    },
    "110_NM1_Loop": {
        "110_NM1": {
            NM101: "..",
            NM102: "..",
            NM103: "..",
        }
    }
}
//////************************ */
fun toPack(ship, cases, ordHierarchy, indexOfPacks) = {
    "010_HL": {
        HL01: (indexOfPacks + 1) as String,
        HL02: ordHierarchy as String,
        HL03: "4",
        HL04: "1",
    },
    "110_NM1_Loop": {
        "110_NM1": {
            NM101: "..",
            NM102: cases.HL4.HL4_PERSON_ORG_INDIC,
            NM103: cases.HL4.HL4_DRV_NAME.HL4_DRV_LAST_NAME,
            NM104: cases.HL4.HL4_DRV_NAME.HL4_DRV_FIRST_NAME_MI,
            NM108: "..",
            NM109: cases.HL4.HL4_DRV_DLN
        },
        "130_N3": [{
            N301: cases.HL4.HL4_DRV_ADDRESS
        }],
        "140_N4": {
            N401: cases.HL4.HL4_DRV_CITY,
            N402: cases.HL4.HL4_DRV_STATE,
            N403: cases.HL4.HL4_DRV_ZIP
        }
    },
    "210_IT1_Loop": [{
        "210_IT1": {
            IT102: 1,
            IT103: "..",
            IT104: 0
        },
        "220_SI": [{
            SI01: "..",
            SI02: "..",
            SI03: cases.HL4.HL4_TRAN_CODE_ALPHA
        }],
        "260_REF": [{
            REF01: "IG",
            REF02: cases.HL4.HL4_POLICY_NUM[0 to 7],
        },
                            {
            REF01: "XM",
            REF02: cases.HL4.HL4_DRV_DLN_STATE,
        },
                            {
            REF01: "S3",
            REF02: "V",
        },
                            {
            REF01: "DD",
            REF03: cases.HL4.HL4_POLICY_NUM[8 to 8] default " " ++ cases.HL4.HL4_CYCLE_DATE_CC as String ++ cases.HL4.HL4_CYCLE_DATE_YYMMDD as String,
        }],
        "270_DTM": [{
            DTM01: "222",
            DTM02: cases.HL4.HL4_DRV_DOB_NUM.HL4_DRV_DOB_CC as Date {
                format: "yyMMdd"
            } default "000000",
            DTM05: cases.HL4.HL4_DRV_DOB_NUM.HL4_DRV_DOB_YYMMDD as Number
        },
        {
            DTM01: if (cases.HL4.HL4_TRAN_CODE_ALPHA == "...") "036" else "007",
            DTM02: if (cases.HL4.HL4_TRAN_CODE_ALPHA == "...") cases.HL4.HL4_VEH_EXP_DATE_NUM.HL4_VEH_EXP_DATE_YYMMDD as Date { format: "yyMMdd" } default "000000" else cases.HL4.HL4_VEH_EFF_DATE_NUM.HL4_VEH_EFF_DATE_YYMMDD as Date { format: "yyMMdd" } default "000000",
            DTM05: if (cases.HL4.HL4_TRAN_CODE_ALPHA == "...") cases.HL4.HL4_VEH_EXP_DATE_NUM.HL4_VEH_EXP_DATE_CC as Number else cases.HL4.HL4_VEH_EFF_DATE_NUM.HL4_VEH_EFF_DATE_CC as Number       
        }]
    }]
}
///////************************ */
fun toItem(ship, recItems, packHierarchy, indexOfItems) = {
    "010_HL": {
        // HL01: (4) as String,
        HL01: (indexOfItems + 1) as String,
        HL02: packHierarchy as String,
        HL03: "5",
        HL04: "0"
    // HL04: cases
    },
    "020_LX_Loop": [{
        "020_LX": {
            "LX01": 1
        },
        "025_VEH": {
            "VEH01": 1,
            "VEH02": recItems.HL5_VEH_VIN,
            "VEH03": recItems.HL5_VEH_CC as Number,
            "VEH04": recItems.HL5_VEH_YY as Number {format: "0000"},
            "VEH05": "NA",
            "VEH06": recItems.HL5_VEH_MAKE
        }
    }]
}
---
{
    //Delimiters: "*>U~",
    TransactionSets: {
        v003050: {
            "811": [{
                Group: {
                    GS04: now() as Date {
                        format: "yyMMdd"
                    },
                },
                Heading: {
                    "020_BIG": {
                        BIG01: now() as Date {
                            format: "yyMMdd"
                        },
                        BIG02: "1",
                    },
                    "100_N1_Loop": [{
                        "100_N1": {
                            N101: "..",
                            N102: "...",
                            N103: "..",
                            N104: "..."
                        },
                    },
                    {
                        "100_N1": {
                            N101: "..",
                            N102: "...",
                        }
                    }]
                },
                Detail: {
                    "010_HL_Loop": payload reduce (ship, accShip=[]) -> do {
                        var shipUpd = accShip << toHL1(ship, sizeOf(accShip))
                        var HL1Hierarchy = sizeOf(shipUpd)
                        var itemHierarchy = sizeOf(shipUpd)
                        var ord = payload reduce (ship, accOrd = shipUpd) -> do {
                            var ordUpd =  accOrd << toOrder(ship, HL1Hierarchy)
                            var ordHierarchy = sizeOf(ordUpd)
                            var pack = flatten(payload.Details) reduce (cases, accPkg = ordUpd) -> do {
                                var packUpd = accPkg << toPack(ship, cases, ordHierarchy, sizeOf(accPkg))
                                
                                var packHierarchy = sizeOf(packUpd)         
                                var items = cases.HL5 reduce (recItems, accItems = packUpd) ->
                                                       accItems << toItem(ship, recItems, packHierarchy, sizeOf(accItems))
                                ---
                                items
                            }
                            ---
                            pack
                        }
                        ---
                        ord
                    }
                },
                Summary: {
                    "010_TDS": {
                        TDS01: 1
                    },
                    "110_CTT": {
                        CTT01: sizeOf(flatten(payload.Details).HL5)
 
                    }
                }
            }]
        }
    }
}

有人可以帮助增加和重置每个 H4-H5 循环的 VEH01 计数器。

如果您正在使用 reduce,则输入是一个数组。您可以使用 map() 为数组的每个元素添加索引,然后根据需要使用。

由于问题的转换太复杂而无法尝试修改我将进行有根据的猜测来做一个简单的例子:

%dw 2.0
output application/json
---
payload.HL5 map (($) ++ {index: $$ + 1})

输入:

{
    "HL5": [
          {
            "HL5_VEH_VIN": "1GJHHKHGJGJGJG878",
            "HL5_VEH_MAKE": "JGKJGJH",
            "HL5_VEH_CC": "20",
            "HL5_VEH_YY": "15"
          },
          {
            "HL5_VEH_VIN": "1JHGJGGKG97968687",
            "HL5_VEH_MAKE": "GJJG",
            "HL5_VEH_CC": "20",
            "HL5_VEH_YY": "22"
          },
          {
            "HL5_VEH_VIN": "186876BJHGJGJ7868",
            "HL5_VEH_MAKE": "GJGKHKH",
            "HL5_VEH_CC": "20",
            "HL5_VEH_YY": "20"
          }
    ]
}

输出:

[
  {
    "HL5_VEH_VIN": "1GJHHKHGJGJGJG878",
    "HL5_VEH_MAKE": "JGKJGJH",
    "HL5_VEH_CC": "20",
    "HL5_VEH_YY": "15",
    "index": 1
  },
  {
    "HL5_VEH_VIN": "1JHGJGGKG97968687",
    "HL5_VEH_MAKE": "GJJG",
    "HL5_VEH_CC": "20",
    "HL5_VEH_YY": "22",
    "index": 2
  },
  {
    "HL5_VEH_VIN": "186876BJHGJGJ7868",
    "HL5_VEH_MAKE": "GJGKHKH",
    "HL5_VEH_CC": "20",
    "HL5_VEH_YY": "20",
    "index": 3
  }
]

您需要确定在什么时候需要应用转换并使用添加的索引属性。也许在这个表达式之间:var items = cases.HL5 /* here? */ reduce ...