如何在mule中重复数据编织内的多个列表

时间:2016-07-22 15:46:20

标签: json list mule dataweave

我面临的问题是在dataweave中重复多个列表。

数据编织的输入 - >进入两个流量变量(如下),

var1: [{bkAuthorID=1, bkAuthorName=abc, bookCategory=horror}, {bkAuthorID=2, bkAuthorName=xyz, bookCategory=horror}, {bkAuthorID=1, bkAuthorName=abc, bookCategory=comedy}, {bkAuthorID=2, bkAuthorName=xyz, bookCategory=Fantacy}}]

var2: [{bkAuthorID=1, bkName=gjh, bkDescription=kjh},{bkAuthorID=1, bkName=sfs, bkDescription=kjh},{bkAuthorID=2, bkName=gjh, bkDescription=kjh},{bkAuthorID=2, bkName=gjh, bkDescription=kjh}]

dataweave中的输出 - >所需的json结构有三个列表,如下所示。

{
  "books": [
    {
      "bookAuthor": "abc",
      "bookCategorys": [
        {
          "bookCategory": "horror",
          "listOfBooks": [
            {
              "bookName": "xyz",
              "bookDescription":"lmn"
            },
            {
              "bookName": "xyz",
              "bookDescription":"lmn"
            }
          ]
        },
        {
          "bookCategory": "comedy",
          "listOfBooks": [
            {
              "bookName": "xyz",
              "bookDescription":"lmn"
            }
          ]
        }
      ]
    },
    {
      "bookAuthor": "xyz",
      "bookCategorys": [
        {
          "bookCategory": "horror",
          "listOfBooks": [
            {
              "bookName": "xyz",
              "bookDescription":"lmn"
            }
          ]
        }
      ]
    }
  ]
}

任何人都可以根据此要求为此数据编辑映射提供帮助。

@Manik,能帮帮我吗。

1 个答案:

答案 0 :(得分:0)

我能够使用bkAuthorID在var1和var2之间建立关系。 但是,必须有一个关系来判断var2中的哪本书属于哪个类别。因此,在下面的输出中,每个类别将包含该作者所写的所有书籍的列表。无论如何,您只需要在“listOfBooks”中添加一个过滤条件即可。线。

Dataweave代码:

%dw 1.0
%output application/json
---
{
    books:( flowVars.var1 groupBy $.bkAuthorName map using (authorId=$.bkAuthorID[0]) {
        bookAuthor:$[0].bkAuthorName,
        bookCategorys: $ groupBy $.bookCategory map {
            bookCategory:$.bookCategory[0],
            listOfBooks: flowVars.var2[?($.bkAuthorID == authorId)] map {
                bookName:$.bkName,
                bookDescription:$.bkDescription
            }
        }
    })
}

<强>输出:

{
"books": [
{
  "bookAuthor": "abc",
  "bookCategorys": [
    {
      "bookCategory": "horror",
      "listOfBooks": [
        {
          "bookName": "gjh",
          "bookDescription": "kjh"
        },
        {
          "bookName": "sfs",
          "bookDescription": "kjh"
        }
      ]
    },
    {
      "bookCategory": "comedy",
      "listOfBooks": [
        {
          "bookName": "gjh",
          "bookDescription": "kjh"
        },
        {
          "bookName": "sfs",
          "bookDescription": "kjh"
        }
      ]
    }
  ]
},
{
  "bookAuthor": "xyz",
  "bookCategorys": [
    {
      "bookCategory": "horror",
      "listOfBooks": [
        {
          "bookName": "gjh",
          "bookDescription": "kjh"
        },
        {
          "bookName": "gjh",
          "bookDescription": "kjh"
        }
      ]
    },
    {
      "bookCategory": "Fantacy",
      "listOfBooks": [
        {
          "bookName": "gjh",
          "bookDescription": "kjh"
        },
        {
          "bookName": "gjh",
          "bookDescription": "kjh"
        }
      ]
    }
  ]
 }
 ]
 }