首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用DataWeave实现两个有效负载

使用DataWeave实现两个有效负载
EN

Stack Overflow用户
提问于 2021-10-21 09:43:53
回答 1查看 42关注 0票数 0

我是MuleSoft新手,正在为DataWeave的一个场景而苦苦挣扎。有人能帮我找到一个优化的方法来合并两个有效负载以获得所需的输出吗? payload1有字段,payload2有这些字段的规则集;我们需要用相应的规则来组合每个字段;例如,“name”字段需要用“name”规则组合,类似的“roll”字段也需要用“roll”规则组合(它们可能是单个字段的多个规则);等等:

数据:

payload1 =

代码语言:javascript
复制
    [
        {
            "ID" : "1",
            "Name" : "ABC",
            "Roll" : 123,
            "Address" : "PQR-234",
            "Standard" : "5"
        },
        {
            "ID" : "2",
            "Name" : "PQR",
            "Roll" : 456,
            "Address" : "REC-678",
            "Standard" : "7"
        },.
        .
        .
    ]

payload2 =

代码语言:javascript
复制
    [
        {
            "field" : "Name",
            "field-Type": "String",
            "operator": "Not-In-List" ,
            "condition-Operand-Type": "",
            "error-Message": "Name - Invalid Value: Name not found in the list."
        },
        {
            "field" : "Name",
            "field-Type": "String",
            "operator": "invalid" ,
            "condition-Operand-Type": "",
            "error-Message": "Name - Invalid Value: There cannot be special char in a name."
        },
        {
            "field" : "Roll",
            "field-Type": "String",
            "operator": "Not-In-List" ,
            "condition-Operand-Type": "",
            "error-Message": "Roll - Invalid Value: Roll not found in the list."
        },
        {
            "field" : "Address",
            "field-Type": "String",
            "operator": "Not-In-List" ,
            "condition-Operand-Type": "",
            "error-Message": "Address - Invalid Value: Address not found in the list."
        },
        .
        .
        .
    ]

所需输出:

代码语言:javascript
复制
    [
        {
            "ID" : "1",
            "Name" : "ABC",
            "data-Quality-Rule" : 
            {
                "field" : "ABC",
                "field-Type": "String",
                "operator": "Not-In-List" ,
                "condition-Operand-Type": "",
                "error-Message": "Name - Invalid Value: Name not found in the list."
            }
        },
        {
            "ID" : "1",
            "Name" : "ABC",
            "data-Quality-Rule" : 
            {
                "field" : "ABC",
                "field-Type": "String",
                "operator": "invalid" ,
                "condition-Operand-Type": "",
                "error-Message": "Name - Invalid Value: There cannot be special char in a name."
            }
        },
        {
            "ID" : "1",
            "Roll" : "123",
            "data-Quality-Rule" : 
            {
                "field" : "123",
                "field-Type": "String",
                "operator": "Not-In-List" ,
                "condition-Operand-Type": "",
                "error-Message": "Roll - Invalid Value: Roll not found in the list."
            }
        },
        .
        .
        .
        
    ]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-21 17:57:22

首先,使用这些键过滤应用于有效负载上的每个元素的规则列表。我使用了namesOf(),但也可以使用pluck()来完成。然后使用列表中每一项上的规则列表来映射输出,而不是尝试在原始有效负载上执行此操作。并使用flatMap()而不是map()来移除每个步骤中不方便的列表嵌套。

脚本:

代码语言:javascript
复制
%dw 2.0
output application/json
fun applyRules(p, r)=
    namesOf(p) flatMap ((keyName, order) -> rules filter ($.field == keyName as String))
---
payload flatMap ((item, order) -> 
    applyRules(item, vars.rules) 
    map {
            ID: item.ID, 
            Name: item.Name, 
            "data-quality-rules": $
        }
    ) 

有效负载:

代码语言:javascript
复制
[
    {
        "ID" : "1",
        "Name" : "ABC",
        "Roll" : 123,
        "Address" : "PQR-234",
        "Standard" : "5"
    },
    {
        "ID" : "2",
        "Name" : "PQR",
        "Roll" : 456,
        "Address" : "REC-678",
        "Standard" : "7"
    }
]

变量vars.rules:

代码语言:javascript
复制
[
    {
        "field" : "Name",
        "field-Type": "String",
        "operator": "Not-In-List" ,
        "condition-Operand-Type": "",
        "error-Message": "Name - Invalid Value: Name not found in the list."
    },
    {
        "field" : "Name",
        "field-Type": "String",
        "operator": "invalid" ,
        "condition-Operand-Type": "",
        "error-Message": "Name - Invalid Value: There cannot be special char in a name."
    },
    {
        "field" : "Roll",
        "field-Type": "String",
        "operator": "Not-In-List" ,
        "condition-Operand-Type": "",
        "error-Message": "Roll - Invalid Value: Roll not found in the list."
    },
    {
        "field" : "Address",
        "field-Type": "String",
        "operator": "Not-In-List" ,
        "condition-Operand-Type": "",
        "error-Message": "Address - Invalid Value: Address not found in the list."
    }
]

输出:

代码语言:javascript
复制
[
  {
    "ID": "1",
    "Name": "ABC",
    "data-quality-rules": {
      "field": "Name",
      "field-Type": "String",
      "operator": "Not-In-List",
      "condition-Operand-Type": "",
      "error-Message": "Name - Invalid Value: Name not found in the list."
    }
  },
  {
    "ID": "1",
    "Name": "ABC",
    "data-quality-rules": {
      "field": "Name",
      "field-Type": "String",
      "operator": "invalid",
      "condition-Operand-Type": "",
      "error-Message": "Name - Invalid Value: There cannot be special char in a name."
    }
  },
  {
    "ID": "1",
    "Name": "ABC",
    "data-quality-rules": {
      "field": "Roll",
      "field-Type": "String",
      "operator": "Not-In-List",
      "condition-Operand-Type": "",
      "error-Message": "Roll - Invalid Value: Roll not found in the list."
    }
  },
  {
    "ID": "1",
    "Name": "ABC",
    "data-quality-rules": {
      "field": "Address",
      "field-Type": "String",
      "operator": "Not-In-List",
      "condition-Operand-Type": "",
      "error-Message": "Address - Invalid Value: Address not found in the list."
    }
  },
  {
    "ID": "2",
    "Name": "PQR",
    "data-quality-rules": {
      "field": "Name",
      "field-Type": "String",
      "operator": "Not-In-List",
      "condition-Operand-Type": "",
      "error-Message": "Name - Invalid Value: Name not found in the list."
    }
  },
  {
    "ID": "2",
    "Name": "PQR",
    "data-quality-rules": {
      "field": "Name",
      "field-Type": "String",
      "operator": "invalid",
      "condition-Operand-Type": "",
      "error-Message": "Name - Invalid Value: There cannot be special char in a name."
    }
  },
  {
    "ID": "2",
    "Name": "PQR",
    "data-quality-rules": {
      "field": "Roll",
      "field-Type": "String",
      "operator": "Not-In-List",
      "condition-Operand-Type": "",
      "error-Message": "Roll - Invalid Value: Roll not found in the list."
    }
  },
  {
    "ID": "2",
    "Name": "PQR",
    "data-quality-rules": {
      "field": "Address",
      "field-Type": "String",
      "operator": "Not-In-List",
      "condition-Operand-Type": "",
      "error-Message": "Address - Invalid Value: Address not found in the list."
    }
  }
]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69659596

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档