UDFs - List of JSON objects to CSV string transformation

This sample UDF takes a List of JSON objs as input and returns a CSV stored as a string of Text. The CSV output can be copied and saved in a text editor as *.csv or the Create File data operation can be used to create a CSV file directly in Airkit.

Given the following json input (dataType: List of <JSON>):

[
    {
        "my": "this",
        "csv": "is",
        "headers": "one row"
    },
    {
        "my": "this",
        "csv": "is",
        "headers": "another row"
    },
    {
        "my": "and this",
        "csv": "here is",
        "headers": "yet another row"
    }
]

We use this transformation in a UDF’s Function Body to transform it into a CSV string (json is referring to the json payload above).

"{{JOIN(
  KEYS(
    json[0]
  ),
  ","
)}}\n{{JOIN(
  FROM obj IN json SELECT JOIN(VALUES(obj), ","),
  "\n"
)}}"

To get this CSV output (dataType: Text):

my,csv,headers
this,is,one row
this,is,another row
and this,here is,yet another row

This can easily be tested in a Data Flow using a Transform data operation:

1 Like