Mastering DataWeave’s Zip and Unzip Functions: A Step-by-Step Guide

Zip and Unzip both are Dataweave pre-defined functions which are part of Core (dw::Core) Library.

Zip | zip(Array<T>, Array<R>): Array<Array<T | R>>

It is used to combine two arrays based on their index positions and results in an array of arrays. It will skip the array value if the index is not present or matched in another array or vice-versa. For example, it skipped “4” presented in digits array from the output response as numbers only contain 3 index positions.

Example
%dw 2.0
output application/json

var numbers = ["One", "Two", "Three"]
var digits = ["1","2","3","4"]
---
//Combining two arrays using zip function, it can also be written as zip(numbers, digits)
numbers zip digits
Output
[
  [
    "One",
    "1"
  ],
  [
    "Two",
    "2"
  ],
  [
    "Three",
    "3"
  ]
]
Unzip |unzip(Array<Array<T>>): Array<Array<T>>

Unzip is a reverse operation of zip, which accepts input as an array of arrays and results into the first index of each sub-array to one array and so on.

Example
%dw 2.0
output application/json

var numbers = ["One", "Two", "Three"]
var digits = ["1","2","3","4"]
---
//perform unzip on above zip output response
unzip(numbers zip digits)
Output
[
  [
    "One",
    "Two",
    "Three"
  ],
  [
    "1",
    "2",
    "3"
  ]
]