Fold Transform

The fold transform collapses (or “folds”) one or more data fields into two properties: a key property (containing the original data field name) and a value property (containing the data value).

The fold transform is useful for mapping matrix or cross-tabulation data into a standardized format, acting as an inverse to the pivot transform.

This transform generates a new data stream in which each data object consists of the key and value properties as well as all the original fields of the corresponding input data object.

Note: The fold transform only applies to a list of known fields (set using the fields parameter). If your data objects instead contain array-typed fields, you may wish to use the flatten transform instead.

Transform Parameters

Property Type Description
fields Field[ ] Required. An array of data fields indicating the properties to fold.
as String[ ] The output field names for the key and value properties produced by the fold transform. The default is ["key", "value"].

Usage

{"type": "fold", "fields": ["gold", "silver"]}

This example folds the gold and silver properties. Given the input data

[
  {"country": "USA", "gold": 10, "silver": 20},
  {"country": "Canada", "gold": 7, "silver": 26}
]

this example produces the output:

[
  {"key": "gold", "value": 10, "country": "USA", "gold": 10, "silver": 20},
  {"key": "silver", "value": 20, "country": "USA", "gold": 10, "silver": 20},
  {"key": "gold", "value": 7, "country": "Canada", "gold": 7, "silver": 26},
  {"key": "silver", "value": 26, "country": "Canada", "gold": 7, "silver": 26}
]