Nav

Example: Zip Arrays Together

This example restructures bills of materials for Ikea-style furniture. The input contains the measurements and amounts of screws in two separate arrays that run in parallel, the transformation reorders them so that the "screws" array is made up of tuples, each with a measurement and its corresponding amount. The same is applied to wooden boards: the input contains two arrays with the x and the y measurements of each; the transformation rearranges them into a series of tuples, one for each board.

It uses these DataWeave functions:

  • map to go through the elements in the main array.

  • zip to rearrange pairs of long arrays, so that they’re grouped by index into multiple two-element arrays.

DataWeave

         
      
1
2
3
4
5
6
7
8
9
10
%dw 2.0
output application/json
---
payload map (item, index) ->
{
    name: item.name,
    id: item.itemID,
    screws: zip(item.screws.size, item.screws.quantity),
    measurements: zip(item.measurements.x,item.measurements.y )
}
Input

         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[
   {
    "name":"wooden-chair",
    "itemID": "23665",
    "screws":{
      "size":[4,6,10],
      "quantity":[15,8,28]
    },
    "measurements":
    {"x":[25,46, 46, 16,150,5, 100, 100, 8],
    "y":[15,4, 4, 80,3, 4, 4, 15]
    }
  },
   {
    "name":"cofee-table",
    "itemID": "14398",
    "screws":{
      "size":[3,8,10],
      "quantity":[8,12,20]
    },
    "measurements":
    {"x":[55, 48, 48, 48, 48, 30, 30, 30, 30],
    "y":[55, 40, 40, 40, 50, 4, 4, 4, 4]
    }
  }
]
Output

         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[
  {
    "name": "wooden-chair",
    "id": "23665",
    "screws": [
      [4, 15],
      [6, 8],
      [10, 28]
    ],
    "measurements": [
      [25,  15],
      [46, 4],
      [46,  4],
      [16,  80],
      [150,  3],
      [5,  4],
      [100,  4],
      [100,  15]
    ]
  },
  {
    "name": "cofee-table",
    "id": "14398",
    "screws": [
      [3, 8],
      [8, 12],
      [10, 20]
    ],
    "measurements": [
      [55, 55],
      [48, 40 ],
      [48, 40],
      [48, 40],
      [48,50],
      [30, 4],
      [30, 4],
      [30, 4],
      [30, 4]
    ]
  }
]

In this topic: