Different pipes. One standard.

Type to search this site.

Search runs in your browser against a static index built with this site (Pagefind). Nothing you type is sent to a server.

TransformExperimental

std:csv-encode #

Serialises records as CSV text.

For example: give a result to someone who wants a CSV file.

A pipe is a data processing step with declared inputs and outputs; standard pipes are built into the runner.

Ports #

std:csv-encode takes in and produces out.std:csv-encodeinrecordsouttext
std:csv-encode takes in and produces out.std:csv-encodeinrecordsouttext
Named ports from the latest published contract. Dashed lines are optional ports.
Ports of std:csv-encode
PortDirectionKindRequiredDescription
ininputrecordsyes
outoutputtextyes Media types: text/csv, text/plain; charset=utf-8

Parameters #

Parameters of std:csv-encode
NameTypeDefaultRequiredDescription
headerbooleantrueno
delimiterstring","no
fieldsarraynonenoColumn names. Default is the first record's member names.

When to use it #

  • The last step of a report: records in, CSV text with a header row out.
  • Choose and order the columns explicitly with fields; members that are not listed are left out.

When to use something else

  • CSV loses types — everything becomes text, and null and an empty string look the same. A member that holds an array or an object fails the run: flatten it first with std:map.

A tested example #

Nothing here is invented: this is the test “columns from the first record” from the pipe's own portable test file, which every conformant runner must pass.

Parameters

This test sets no parameters.

Input: in
{"id":1,"name":"Bond, James"}
{"name":"Q","id":2}
Expected output: out
id,name
1,"Bond, James"
2,Q

The same step as a node of a pipeline.yaml:

pipeline.yaml
nodes:
  csv-encode:
    pipe: std:csv-encode
    inputs:
      in: {from: inputs.in}

Run it #

The pipe runs on its own from a shell. The input is the test input above. The quickstart shows how to get the openflange command.

Command
printf '%s\n' '{"id":1,"name":"Bond, James"}' '{"name":"Q","id":2}' \
  | openflange run std:csv-encode
Standard output (out)
id,name
1,"Bond, James"
2,Q

Executed with openflange 0.0.1 (go1.27.1 linux/amd64), built from commit 11cf8cc of the reference implementation; the output shown is what it printed, and it matches the test.

Notes from the manifest #

The pipe's manifest, OPENFLANGE.md, carries these notes.

Part of the OpenFlange standard library (std, version 0.1).

The explicit — and lossy with respect to types — conversion from records to CSV text (it is never inserted automatically: in CSV everything is text, and null and "" coincide).

Columns are fields, or the member names of the first record in their order. With header: true (default) a header row is written first.

  • Rows end with LF (\n), including the last one.
  • A string is written as it is, quoted with " (embedded quotes doubled) only when it contains the delimiter, a quote, CR or LF.
  • A number is written as its original literal; true/false as those words; null and absent members as an empty field.
  • A member holding an array or an object fails the run (E_RUNTIME): CSV cannot represent it losslessly, convert it first.
  • When the columns came from the first record, a later record with a member that is not a column fails the run instead of losing it. With fields, other members are ignored (an explicit projection).
  • An item that is not an object fails.
  • Empty input: with fields and header: true only the header row is written; otherwise the output is empty.

CSV injection. Values beginning with =, +, - or @ can be evaluated as formulas by spreadsheet software. The encoder never alters data to prevent this; sanitise before handing CSV to spreadsheet users.

Reference #

Maturity
Experimental
Processing scope
record
Manifest version
0.1.0