需要根据对应的对象组合两个payload

Need to combine two payloads according to corresponding objects

有没有办法将下面提到的有效载荷结合起来。我们希望 payload2 中的 'address' 根据更新了 'result' 值的 'uniqueID' 放入 payload1.errors 中。 如果 payload2 在 payload1.errors 中有没有对应 uniqueID 的对象,输出仍然应该有带有地址字段和 uniqueID 的对象,例如 uniqueID = 4 in payload2.

payload1.errors 中的字段将是 payload2 中字段的子集,'address' 字段除外。

请注意:这些只是示例,实际负载有超过 10 个对象,每个对象有超过 50 个字段。

有效载荷1:

 {
  "metadata": {
    "SchoolName": "ABC High School",
    "quarter": "2021 Q4",
    "ID": "4sdfsdd-esdf-esad1-8asfs7"
  },
  "result": "data-validation-error",
  "errors": [
    {
      "uniqueID": "1",
      "standard": "Must Be Equal to V",
      "marks": "Must Be Equal to 90+",
      "rollNumber": "Must Be Between 1 and 10"
    },
    {
      "uniqueID": "2",
      "standard": "Must Be Equal to VI",
      "marks": "Must Be Equal to 80+",
      "rollNumber": "Must Be Between 10 and 20"
    },
    {
      "uniqueID": "3",
      "standard": "Must Be Equal to VII",
      "marks": "Must Be Equal to 85+",
      "rollNumber": "Must Be Between 30 and 40"
    },
    {
      "uniqueID": "5",
      "standard": "Must Be Equal to XI",
      "marks": "Must Be Equal to 90+",
      "rollNumber": "Must Be Between 50 and 60"
    }
  ]
}

第二个负载是:

有效载荷2:

[
  {
    "uniqueID": "1",
    "address": "bcod4001c",
    "admissionDate": "2025-03-28",
    "parentDetails": "N/A",
    "standard": "VIII",
    "marks": "80",
    "rollNumber": "14"
    
  },
  {
    "uniqueID": "2",
    "address": "bhoe4001d",
    "admissionDate": "2021-04-21",
    "parentDetails": "N/A",
    "standard": "IX",
    "marks": "76",
    "rollNumber": "4"
  },
  {
    "uniqueID": "3",
    "address": "bmor4001e",
    "admissionDate": "2021-03-28",
    "parentDetails": "N/A",
    "standard": "IV",
    "marks": "75",
    "rollNumber": "9"
  },
  {
    "uniqueID": "4",
    "address": "bmor7001f",
    "admissionDate": "2021-05-18",
    "parentDetails": "N/A",
    "standard": "X",
    "marks": "96",
    "rollNumber": "45"
  }
  
]

预期输出:

{
      "metadata": {
        "SchoolName": "ABC High School",
        "quarter": "2021 Q4",
        "ID": "4sdfsdd-esdf-esad1-8asfs7"
      },
      "result": "data-validation-error and address validation error",
      "errors": [
        {
          "uniqueID": "1",
          "standard": "Must Be Equal to V",
          "marks": "Must Be Equal to 90+",
          "rollNumber": "Must Be Between 1 and 10",
          "address": "bcod4001c is an invalid address"
        },
        {
          "uniqueID": "2",
          "standard": "Must Be Equal to VI",
          "marks": "Must Be Equal to 80+",
          "rollNumber": "Must Be Between 10 and 20",
          "address": "bhoe4001d is an invalid address"
        },
        {
          "uniqueID": "3",
          "standard": "Must Be Equal to VII",
          "marks": "Must Be Equal to 85+",
          "rollNumber": "Must Be Between 30 and 40",
          "address": "bmor4001e is an invalid address"
        },
        {
          "uniqueID": "4",
          "address": "bmor7001f is an invalid address"
        },
        {
          "uniqueID": "5",
          "standard": "Must Be Equal to XI",
          "marks": "Must Be Equal to 85+",
          "rollNumber": "Must Be Between 40 and 60"
        }
      ]
}

你可以使用Concat运算符++来组合它们 Link : https://docs.mulesoft.com/mule-runtime/3.9/dataweave-operators#concat

您可以使用更新仅转换错误,首先与 payload2 连接,然后根据需要映射每个结果项。

为了简单起见,我使用 payload 1 作为主要 payload,并将 payload2 放入 DataWeave 变量中。

%dw 2.0
output application/json
import * from dw::core::Arrays
import mergeWith from dw::core::Objects
fun getAddressError(address)=if (address != null) {address: address++ " is an invalid address" } else {}
var payload2= [
  {
    "uniqueID": "1",
    "address": "bcod4001c",
    "admissionDate": "2025-03-28",
    "parentDetails": "N/A",
    "standard": "VIII",
    "marks": "80",
    "rollNumber": "14"
    
  },
  {
    "uniqueID": "2",
    "address": "bhoe4001d",
    "admissionDate": "2021-04-21",
    "parentDetails": "N/A",
    "standard": "IX",
    "marks": "76",
    "rollNumber": "4"
  },
  {
    "uniqueID": "3",
    "address": "bmor4001e",
    "admissionDate": "2021-03-28",
    "parentDetails": "N/A",
    "standard": "IV",
    "marks": "75",
    "rollNumber": "9"
  },
  {
    "uniqueID": "4",
    "address": "bmor7001f",
    "admissionDate": "2021-05-18",
    "parentDetails": "N/A",
    "standard": "X",
    "marks": "96",
    "rollNumber": "45"
  } 
]
---
payload update {
        case errors at .errors ->
             outerJoin(payload2, errors, (error1) -> error1.uniqueID, (error2) -> error2.uniqueID)
               map (
                    if ($.r?) ($.r ++ getAddressError($.l.address) ) 
                    else {uniqueId: $.l.uniqueID, (getAddressError($.l.address))}
               ) 
}

输出:

{
  "metadata": {
    "SchoolName": "ABC High School",
    "quarter": "2021 Q4",
    "ID": "4sdfsdd-esdf-esad1-8asfs7"
  },
  "result": "data-validation-error",
  "errors": [
    {
      "uniqueID": "1",
      "standard": "Must Be Equal to V",
      "marks": "Must Be Equal to 90+",
      "rollNumber": "Must Be Between 1 and 10",
      "address": "bcod4001c is an invalid address"
    },
    {
      "uniqueID": "2",
      "standard": "Must Be Equal to VI",
      "marks": "Must Be Equal to 80+",
      "rollNumber": "Must Be Between 10 and 20",
      "address": "bhoe4001d is an invalid address"
    },
    {
      "uniqueID": "3",
      "standard": "Must Be Equal to VII",
      "marks": "Must Be Equal to 85+",
      "rollNumber": "Must Be Between 30 and 40",
      "address": "bmor4001e is an invalid address"
    },
    {
      "uniqueId": "4",
      "address": "bmor7001f is an invalid address"
    },
    {
      "uniqueID": "5",
      "standard": "Must Be Equal to XI",
      "marks": "Must Be Equal to 90+",
      "rollNumber": "Must Be Between 50 and 60"
    }
  ]
}