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.

Recipe

CLI and standard pipes: Available — v0.0.1 (experimental)no extra runtime

CSV to cleaned NDJSON

A pipeline of four standard pipes: decode the CSV, trim and normalise every row, validate it, convert strings to numbers and booleans. Bad rows go to a second file with the reason. Parquet is not produced here; the page says what is.

How do I turn a messy CSV file into clean, typed NDJSON?

You have a CSV export with stray spaces, mixed letter case, empty cells and a few rows that are simply wrong. You want one JSON record per line with real numbers and booleans, and you want to see the rows that were refused, not lose them.

A script does this in twenty lines, until the file changes. Here the same job is four standard pipes connected in one file: each has a documented behaviour, and the whole pipeline has a test.

A pipe is a data-processing step with declared inputs and outputs; a pipeline connects pipes.

What you need

  • The openflange command, release v0.0.1:
    bash
    go install github.com/openflange/openflange-go/cmd/openflange@v0.0.1
  • An empty directory. Create the files below in it under exactly these names and run every command from that directory.
  • Nothing else: every step of this recipe runs inside the runner. No network, no account, no key.

Steps

1. Save the input file

Five contacts. Row 1 has spaces around the name and an upper-case address, row 2 has no age, row 3 has a comma inside a quoted name, rows 4 and 5 are wrong: one address has no @, one age is a word.

contacts.csv
id,name,email,age,newsletter
1,  Ada Lovelace ,ADA@Example.com,36,true
2,Grace Hopper,grace@example.org,,false
3,"Turing, Alan",Alan@Example.COM,41,true
4,Linus,linus-at-example.net,55,true
5,Margaret Hamilton,margaret@example.com,thirty,false

2. Describe the pipeline and its contract

pipeline.yaml connects the pipes; OPENFLANGE.md declares what goes in and what comes out, so the directory can be run, tested and reused as one pipe. Every CSV value arrives as a string: clean tidies the strings, check validates them, typed converts them.

pipeline.yaml
# csv ─▶ decode ─▶ clean ─▶ check ─┬─▶ typed ─▶ records
#                                  └─▶ rejected
spec: openflange.org/pipeline/v0.1
nodes:
  decode:
    pipe: std:csv-decode          # every value arrives as a string
    inputs:
      in: {from: inputs.csv}
  clean:
    pipe: std:map                 # trim the name, lower-case the e-mail, empty age -> null
    inputs:
      in: {from: decode.outputs.out}
    parameters:
      expr: 'dict(item, name=item["name"].strip(), email=item["email"].strip().lower(), age=item["age"].strip() or None)'
  check:
    pipe: std:validate-schema     # rows that do not match go to `rejected`, with the reasons
    inputs:
      in: {from: clean.outputs.out}
    parameters:
      schema:
        type: object
        required: [id, name, email, age, newsletter]
        properties:
          id: {type: string, pattern: "^[0-9]+$"}
          name: {type: string, minLength: 1}
          email: {type: string, pattern: "^[^@ ]+@[^@ ]+$"}
          age: {type: ["string", "null"], pattern: "^[0-9]+$"}
          newsletter: {enum: ["true", "false"]}
  typed:
    pipe: std:cast                # strings -> JSON types, exactly or not at all
    inputs:
      in: {from: check.outputs.valid}
    parameters:
      fields: {id: integer, age: integer, newsletter: boolean}
outputs:
  records: {from: typed.outputs.out}
  rejected: {from: check.outputs.rejected}
OPENFLANGE.md
---
spec: openflange.org/pipe/v0.1
id: csv-to-ndjson
version: 0.1.0
title: CSV to cleaned NDJSON
description: Reads a contacts CSV, trims and normalises every row, validates it and writes typed records as NDJSON; rows that fail validation are routed to `rejected`.
license: Apache-2.0
keywords: [csv, ndjson, clean, validate, recipe]
maturity: experimental
scope: record
inputs:
  csv:
    kind: text
    mediaTypes: [text/csv]
    description: 'Contacts with a header row: id,name,email,age,newsletter.'
outputs:
  records:
    description: 'One record per valid row: {"id": integer, "name", "email", "age": integer or null, "newsletter": boolean}.'
  rejected:
    required: false
    description: 'Rows that fail validation, as {"item", "errors"}. If not connected, the first invalid row fails the run.'
implementation:
  type: pipeline
  path: pipeline.yaml
---
# CSV to cleaned NDJSON

Implements the [OpenFlange](https://openflange.org/) specification.

CSV in, NDJSON out: decode, clean, validate, cast. Every node is a standard
pipe and runs in-process, one row at a time; no permissions are needed.

3. Validate and run

Validation checks the files and the connections without reading any data. The run binds the input to the CSV file and the two outputs to two files.

bash
openflange validate .
Output
valid: . (4 nodes, 6 edges)
bash
openflange run . --input csv=contacts.csv --output records=contacts.ndjson --output rejected=rejected.ndjson
Output
completed in 560µs — records: 3 records, rejected: 2 records
contacts.ndjson (written by the command)
{"id":1,"name":"Ada Lovelace","email":"ada@example.com","age":36,"newsletter":true}
{"id":2,"name":"Grace Hopper","email":"grace@example.org","age":null,"newsletter":false}
{"id":3,"name":"Turing, Alan","email":"alan@example.com","age":41,"newsletter":true}
rejected.ndjson (written by the command)
{"item":{"id":"4","name":"Linus","email":"linus-at-example.net","age":"55","newsletter":"true"},"errors":[{"path":"/email","message":"'linus-at-example.net' does not match pattern '^[^@ ]+@[^@ ]+$'"}]}
{"item":{"id":"5","name":"Margaret Hamilton","email":"margaret@example.com","age":"thirty","newsletter":"false"},"errors":[{"path":"/age","message":"'thirty' does not match pattern '^[0-9]+$'"}]}

Three rows became typed records: id and age are numbers, newsletter is a boolean, the empty age is null. Two rows were refused, each with the original row and the reason.

4. See what happens without a place for bad rows

Leave the rejected output unbound and the first bad row stops the run with a non-zero exit status. Records are never dropped silently.

bash
openflange run . --input csv=contacts.csv
Output (exit status 1)
E_RUNTIME: item 4 does not match the schema: /email: 'linus-at-example.net' does not match pattern '^[^@ ]+@[^@ ]+$' (node check)
  hint: connect the output `rejected` to receive invalid items as data instead of failing
failed in 1ms — records: 0 records

5. Optional: typed columns as an Arrow stream

This runner has the optional arrow capability, so the standard pipe std:ndjson-to-arrow can write the records as an Arrow IPC stream with an explicit schema, and std:arrow-to-ndjson reads it back. This is Arrow, not Parquet.

bash
openflange run std:ndjson-to-arrow --quiet --param 'schema={"fields":[{"name":"id","type":"int64","nullable":false},{"name":"name","type":"utf8"},{"name":"email","type":"utf8"},{"name":"age","type":"int32"},{"name":"newsletter","type":"bool"}]}' < contacts.ndjson > contacts.arrow

This command prints nothing.

bash
openflange run std:arrow-to-ndjson --quiet < contacts.arrow
Output
{"id":1,"name":"Ada Lovelace","email":"ada@example.com","age":36,"newsletter":true}
{"id":2,"name":"Grace Hopper","email":"grace@example.org","age":null,"newsletter":false}
{"id":3,"name":"Turing, Alan","email":"alan@example.com","age":41,"newsletter":true}

6. Test it

The test file sits beside the pipeline and needs nothing but the CLI. It checks the happy path, the bad rows, a file with only a header row and the strict failure.

tests/csv-to-ndjson.test.yaml
spec: openflange.org/test/v0.1
pipe: ..
tests:
  - name: trims, lower-cases and types every valid row
    inputs:
      csv:
        text: |
          id,name,email,age,newsletter
          1,  Ada Lovelace ,ADA@Example.com,36,true
          2,Grace Hopper,grace@example.org,,false
    expect:
      outputs:
        records:
          values:
            - {id: 1, name: Ada Lovelace, email: ada@example.com, age: 36, newsletter: true}
            - {id: 2, name: Grace Hopper, email: grace@example.org, age: null, newsletter: false}
        rejected: {count: 0}

  - name: a row with a bad e-mail or a bad age is routed to rejected with the reasons
    inputs:
      csv:
        text: |
          id,name,email,age,newsletter
          4,Linus,linus-at-example.net,55,true
          5,Margaret Hamilton,margaret@example.com,thirty,false
    expect:
      outputs:
        records: {count: 0}
        rejected:
          count: 2
          schema:
            type: object
            required: [item, errors]
            properties:
              errors: {type: array, minItems: 1, items: {type: object, required: [path, message]}}

  - name: a header row alone gives no records
    inputs:
      csv: {text: "id,name,email,age,newsletter\n"}
    expect:
      outputs:
        records: {count: 0}
        rejected: {count: 0}

  - name: without a rejected consumer a bad row fails the run
    inputs:
      csv: {text: "id,name,email,age,newsletter\n4,Linus,linus-at-example.net,55,true\n"}
    expect:
      status: failed
      error: {code: E_RUNTIME}
      outputs:
        rejected: {disabled: true}
bash
openflange test .
Output
tests/csv-to-ndjson.test.yaml
  ok    trims, lower-cases and types every valid row
  ok    a row with a bad e-mail or a bad age is routed to rejected with the reasons
  ok    a header row alone gives no records
  ok    without a rejected consumer a bad row fails the run
ok 4 tests passed in 1 file (19ms)

Limits

  • No Parquet. No standard pipe in std 0.1 reads or writes Parquet, and the reference runner has no Parquet output. What was run here is an Arrow IPC stream (application/vnd.apache.arrow.stream). Turning that stream into a Parquet file needs another tool, and that step was not run for this page.
  • Arrow is an optional capability. Check openflange version --capabilities: a runner without arrow refuses the two Arrow pipes with E_UNSUPPORTED.
  • Nothing is guessed. std:csv-decode emits strings only, and std:cast converts exactly or not at all: " 42" and "1.5" are not integers. That is why clean trims before typed casts.
  • A row with the wrong number of fields is not a bad record but a broken file: the run fails with E_FORMAT and the row does not go to rejected.
  • JSON Schema format is not checked by std:validate-schema; use pattern, as this recipe does.
  • The pipeline handles one row at a time and keeps no state, so memory does not grow with the file. The fixture here has five rows; no large file was measured for this page.
  • For a one-off conversion of a file that is already clean, a single command in a tool you already use is enough.

Every command on this page was run on Linux with openflange 0.0.1 (go1.27.1 linux/amd64), built from openflange-go commit 11cf8cc5378d70858724b16aa937d7f3b98c085d. The files and the recorded output are kept in the site repository under sites/dev/test/fixtures/recipes/csv-to-ndjson/, and a test compares this page with them byte for byte.