Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

dataweave - Where do I place the groupBy when using pluck

I am working my way through the DataWeave tutorial (highly recommended!) at dwlang.fun and I am finally stumped by the pluck section of the working with objects chapter. The input is a single array of four order lines that I need to group by order id. The input is

[
  {
    "orderId"  : 1,
    "customer" : "Josh",
    "lineId"   : 1,
    "lineItem" : "Shoes",
    "price"    : 50
  },
  {
    "orderId"  : 1,
    "customer" : "Josh",
    "lineId"   : 2,
    "lineItem" : "Socks",
    "price"    : 20
  },
  {
    "orderId"  : 2,
    "customer" : "Mariano",
    "lineId"   : 3,
    "lineItem" : "Shirt",
    "price"    : 30
  },
  {
    "orderId"  : 2,
    "customer" : "Mariano",
    "lineId"   : 4,
    "lineItem" : "Jacket",
    "price"    : 80
  }
]

and the desired output is a single array containing each order in its own array:

[
  [
    {
      "orderId": 1,
      "customer": "Josh",
      "lineId": 1,
      "lineItem": "Shoes",
      "price": 50
    },
    {
      "orderId": 1,
      "customer": "Josh",
      "lineId": 2,
      "lineItem": "Socks",
      "price": 20
    }
  ],
  [
    {
      "orderId": 2,
      "customer": "Mariano",
      "lineId": 3,
      "lineItem": "Shirt",
      "price": 30
    },
    {
      "orderId": 2,
      "customer": "Mariano",
      "lineId": 4,
      "lineItem": "Jacket",
      "price": 80
    }
  ]
]

My code is close, but the grouping is off. My code is

%dw 2.0
output json
var myData = payload
    map (order, index) -> {
        (order pluck (v, k, idx) -> {
            (k): v
        })
  }
---
myData groupBy ((order, index) -> order.orderId)

and my output is:

{
  "1": [
    {
      "orderId": 1,
      "customer": "Josh",
      "lineId": 1,
      "lineItem": "Shoes",
      "price": 50
    },
    {
      "orderId": 1,
      "customer": "Josh",
      "lineId": 2,
      "lineItem": "Socks",
      "price": 20
    }
  ],
  "2": [
    {
      "orderId": 2,
      "customer": "Mariano",
      "lineId": 3,
      "lineItem": "Shirt",
      "price": 30
    },
    {
      "orderId": 2,
      "customer": "Mariano",
      "lineId": 4,
      "lineItem": "Jacket",
      "price": 80
    }
  ]
}
question from:https://stackoverflow.com/questions/65904987/where-do-i-place-the-groupby-when-using-pluck

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm not sure that myData is useful, I removed it. I used pluck() after groupBy() to collect the values for each group key, which seems to be what is expected:

%dw 2.0
output application/json
---
payload 
    groupBy ((order, index) -> order.orderId)
    pluck ((value, key, index) -> value)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...