Ben
Ben

Reputation: 730

dataweave mapping from an array

I have a basic dataweave mapping function. I want to have a number of objects with a simple array as input: I have created the function generatePoints to achive this


fun generatePoints() = [1 to 24] map {
        currentyear: 2023,
        points: $[$$],
}

It returns a single array:

[
  {
    "currentyear": 2023,
    "points": 1
  }
]

But it should return something like this:

[
  {
    "currentyear": 2023,
    "points": 1
  },
  {
    "currentyear": 2023,
    "points": 2
  },
  {
    "currentyear": 2023,
    "points": 3
  }
.....
  {
    "currentyear": 2023,
    "points": n
  }
]

Does anybody know how to update the generatePoints function to achieve this ?

Upvotes: 0

Views: 349

Answers (2)

Nampd
Nampd

Reputation: 59

A little change of your solution:

    %dw 2.0
output application/json
---
[1 to 24][0] map {
        currentyear: 2023,
        points: $,
}

Result:

[
  {
    "currentyear": 2023,
    "points": 1
  },
  {
    "currentyear": 2023,
    "points": 2
  },
  {
    "currentyear": 2023,
    "points": 3
  },
  {
    "currentyear": 2023,
    "points": 4
  },
  {
    "currentyear": 2023,
    "points": 5
  },
  {
    "currentyear": 2023,
    "points": 6
  },
  {
    "currentyear": 2023,
    "points": 7
  },
  {
    "currentyear": 2023,
    "points": 8
  },
  {
    "currentyear": 2023,
    "points": 9
  },
  {
    "currentyear": 2023,
    "points": 10
  } ...

Upvotes: 0

Harshank Bansal
Harshank Bansal

Reputation: 3262

1 to 24 is already a range with type array. Therefore you have to remove the [] around it. As it is creating an Array with a single "array" element.

Upvotes: 2

Related Questions