Working with CSV, TSV

Encode/Decode/Roundtrip CSV and TSV files.

Encode

Currently supports arrays of homogeneous flat objects, that is: no nesting and it assumes the first object has all the keys required:

- name: Bobo
  type: dog
- name: Fifi
  type: cat

As well as arrays of arrays of scalars (strings/numbers/booleans):

- [Bobo, dog]
- [Fifi, cat]

Decode

Decode assumes the first CSV/TSV row is the header row, and all rows beneath are the entries. The data will be coded into an array of objects, using the header rows as keys.

name,type
Bobo,dog
Fifi,cat

Encode CSV simple

Given a sample.yml file of:

then

will output

Encode TSV simple

Given a sample.yml file of:

then

will output

Encode array of objects to csv

Given a sample.yml file of:

then

will output

Encode array of objects to custom csv format

Add the header row manually, then the we convert each object into an array of values - resulting in an array of arrays. Pick the columns and call the header whatever you like.

Given a sample.yml file of:

then

will output

Encode array of objects to csv - missing fields behaviour

First entry is used to determine the headers, and it is missing 'likesApples', so it is not included in the csv. Second entry does not have 'numberOfCats' so that is blank

Given a sample.yml file of:

then

will output

Parse CSV into an array of objects

First row is assumed to be the header row. By default, entries with YAML/JSON formatting will be parsed!

Given a sample.csv file of:

then

will output

Parse CSV into an array of objects, no auto-parsing

First row is assumed to be the header row. Entries with YAML/JSON will be left as strings.

Given a sample.csv file of:

then

will output

Parse TSV into an array of objects

First row is assumed to be the header row.

Given a sample.tsv file of:

then

will output

Round trip

Given a sample.csv file of:

then

will output

Last updated

Was this helpful?