Search code examples
mule-componentdataweavemule-esbmulesoft

how to groupBy and map at same transform on dataweave 2?


I have this dataweave 1.0 script that works well:

%dw 1.0
%output application/java
---
flowVars.worklogs groupBy $.author.accountId map {
    accountId: $.author.accountId[0],
    displayName: $.author.displayName[0],
    timeSpentMinutesMonth: (sum $.timeSpentSeconds) / 3600,
    billableMinutesMonth: (sum $.billableSeconds) / 3600,
    emailAddress: ''
}

However, now I am updating the code for mule 4, and I couldn't make this transformation goes well.

I already tried to update it like this:

%dw 2.0
output application/java
---
vars.worklogs groupBy $.author.accountId map {
    accountId: $.author.accountId[0],
    displayName: $.author.displayName[0],
    timeSpentMinutesMonth: (sum($.timeSpentSeconds)) / 3600,
    billableMinutesMonth: (sum($.billableSeconds)) / 3600,
    emailAddress: ''
}

But I got this error:

org.mule.runtime.core.internal.message.ErrorBuilder$ErrorImplementation
{
  description="You called the function 'map' with these arguments: 
  1: Object ({"5d8b681427fe990dc2d3404a": [{self: "https://api.tempo.io/core/3/worklogs/54...)
  2: Function ((v:Any, i:Any) -> ???)

But it expects arguments of these types:
  1: Array
  2: Function


 4| vars.worklogs groupBy $.author.accountId map (v, i) -> {
  |  ...
10| }

Trace:
  at map (line: 4, column: 1)
  at main (line: 4, column: 42)" evaluating expression: "%dw 2.0
output application/java
---
vars.worklogs groupBy $.author.accountId map (v, i) -> {
    accountId: v.author.accountId[0],
    displayName: v.author.displayName[0],
    timeSpentMinutesMonth: (sum(v.timeSpentSeconds)) / 3600,
    billableMinutesMonth: (sum(v.billableSeconds)) / 3600,
    emailAddress: ''
}".

The variable worklogs contains a json:

[
  {
    "self": "https://api.tempo.io/core/3/worklogs/5408",
    "tempoWorklogId": 5408,
    "jiraWorklogId": 15408,
    "issue": {
      "self": "https://xpto.atlassian.net/rest/api/2/issue/ABC-123",
      "key": "ABC-123",
      "id": 11005
    },
    "timeSpentSeconds": 28800,
    "billableSeconds": 28800,
    "startDate": "2020-01-31",
    "startTime": "00:00:00",
    "description": "creating new song",
    "createdAt": "2020-02-28T13:30:58Z",
    "updatedAt": "2020-02-28T13:30:58Z",
    "author": {
      "self": "https://xpto.atlassian.net/rest/api/2/user?accountId=5d8b681427fe990dc2d3404a",
      "accountId": "5d8b681427fe990dc2d3404a",
      "displayName": "john lennon"
    },
    "attributes": {
      "self": "https://api.tempo.io/core/3/worklogs/5408/work-attribute-values",
      "values": [
        
      ]
    }
  },
  {
    "self": "https://api.tempo.io/core/3/worklogs/5166",
    "tempoWorklogId": 5166,
    "jiraWorklogId": 15166,
    "issue": {
      "self": "https://xpto.atlassian.net/rest/api/2/issue/CDE-99",
      "key": "CDE-99",
      "id": 10106
    },
    "timeSpentSeconds": 3600,
    "billableSeconds": 3600,
    "startDate": "2020-01-31",
    "startTime": "00:00:00",
    "description": "call with stakeholders",
    "createdAt": "2020-02-10T18:30:03Z",
    "updatedAt": "2020-02-10T18:30:03Z",
    "author": {
      "self": "https://xpto.atlassian.net/rest/api/2/user?accountId=5b27ad3902cfea1ba6411c3f",
      "accountId": "5b27ad3902cfea1ba6411c3f",
      "displayName": "chandler bing"
    },
    "attributes": {
      "self": "https://api.tempo.io/core/3/worklogs/5166/work-attribute-values",
      "values": [
        
      ]
    }
  },
  {
    "self": "https://api.tempo.io/core/3/worklogs/5165",
    "tempoWorklogId": 5165,
    "jiraWorklogId": 15165,
    "issue": {
      "self": "https://xpto.atlassian.net/rest/api/2/issue/CDE-99",
      "key": "CDE-99",
      "id": 10081
    },
    "timeSpentSeconds": 3600,
    "billableSeconds": 3600,
    "startDate": "2020-01-31",
    "startTime": "00:00:00",
    "description": "planning tulsa work trip",
    "createdAt": "2020-02-10T18:29:30Z",
    "updatedAt": "2020-02-10T18:29:30Z",
    "author": {
      "self": "https://xpto.atlassian.net/rest/api/2/user?accountId=5b27ad3902cfea1ba6411c3f",
      "accountId": "5b27ad3902cfea1ba6411c3f",
      "displayName": "chandler bing"
    },
    "attributes": {
      "self": "https://api.tempo.io/core/3/worklogs/5165/work-attribute-values",
      "values": [
        
      ]
    }
  },
  {
    "self": "https://api.tempo.io/core/3/worklogs/5164",
    "tempoWorklogId": 5164,
    "jiraWorklogId": 15164,
    "issue": {
      "self": "https://xpto.atlassian.net/rest/api/2/issue/CDE-99",
      "key": "CDE-99",
      "id": 10108
    },
    "timeSpentSeconds": 7200,
    "billableSeconds": 7200,
    "startDate": "2020-01-31",
    "startTime": "00:00:00",
    "description": "exporting data to cd-rom",
    "createdAt": "2020-02-10T18:29:08Z",
    "updatedAt": "2020-02-10T18:29:47Z",
    "author": {
      "self": "https://xpto.atlassian.net/rest/api/2/user?accountId=5b27ad3902cfea1ba6411c3f",
      "accountId": "5b27ad3902cfea1ba6411c3f",
      "displayName": "chandler-bing"
    },
    "attributes": {
      "self": "https://api.tempo.io/core/3/worklogs/5164/work-attribute-values",
      "values": [
        
      ]
    }
  }
]

I don't understanding why this isn't working. I read the docs and found out that groupBy and map in dw 2.0 works pretty much the same as dw 1.0.


Solution

  • According to this question, it is necessary to add a pluck after groupBy, and not add map:

    %dw 2.0
    output application/json
    ---
    vars.worklogs groupBy $.author.accountId pluck {
        accountId: $.author.accountId[0],
        displayName: $.author.displayName[0],
        timeSpentMinutesMonth: (sum($.timeSpentSeconds)) / 3600,
        billableMinutesMonth: (sum($.billableSeconds)) / 3600,
        emailAddress: ''
    }