简体   繁体   中英

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?

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.

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
  } ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM