- 4 minutes to read

Formula - csv

The csv Formula function returns the values of one column of CSV or other delimited text, after skipping header rows and comment lines.

Cut the order lines out of an Order Confirmation CSV with regex, then read their descriptions with csv, animated walkthrough (7.12)
Animated walkthrough: the Order Confirmation of the Order-to-Cash demo is a CSV file with a header, a details and a footer section. regex('LineNumber,[^\n]*(\n\d+,[^\n]*)+', body) cuts out the order lines with their header row. csv(1, ',', 2, '#', regex(…)) skips that header row and returns column 2 (the third column): Demo Widget, Demo Gadget and Demo Sprocket.

Syntax

csv(headerRowsToSkip, 'delimiter', columnIndex, 'commentSymbol', content)

Parameters

Parameter Required Description
headerRowsToSkip Yes Number of rows to skip at the top, such as a header row. A number, without quotes.
'delimiter' Yes The character that separates columns, such as ',' or ';'.
columnIndex Yes The column to return, zero-based: 0 is the first column, 1 the second. A number, without quotes.
'commentSymbol' Yes Lines that start with this character are ignored, such as '#'.
content Yes The delimited text: body, context, or the result of another function (see Formula language rules).

Returns

One value per remaining row: the value in column columnIndex, with surrounding double quotes removed. Duplicates are collapsed. Rows that are skipped, blank, or start with the comment symbol return nothing.

Examples

Example 1: Return the second column of the message body

Input

"face","suit","value"
"king","spades",13
"queen","spades2",12
#"jack","spades3",11
"ten","spades4",10
"nine","spades5",9
#"twenty","clown",10
"eight","spades6",8
"seven","spades7",7

Formula

csv(2, ',', 1, '#', body)

Result

spades2
spades4
spades5
spades6
spades7

Formula csv expression returning five suit values from a CSV message body in the Test Expression tab
The first two rows (face and king) are skipped, the two # lines are ignored, and column 1 (the second column) is returned.

Example 2: Highest value in a context property

Input

The Message Context key Ids has this value:

"face","suit","value"
"king","spades","13"
"queen","spades2","12"
#"jack","spades3","666"
"ten","spades4","10"

Formula

max(csv(1, ',', 2, '#', context('Ids')))

Result

13

Formula max of csv on the Ids context property returning 13 in the Test Expression tab (7.12)
csv returns 13, 12, and 10 from the third column; the commented 666 row is ignored, so max returns 13.

Example 3: CSV embedded in a JSON field

Input

{
    "name": "JSONFile",
    "description": "TestFile",
    "content": "\"face\",\"suit\",\"value\"\n\"jack\",\"spades3\",11\n\"ten\",\"spades4\",10\n\"nine\",\"spades5\",9\n\"eight\",\"spades6\",8\n\"seven\",\"spades7\",7"
}

Formula

csv(4, ',', 1, '#', jsonPath('$.content', body))

Result

spades6
spades7

Formula csv over jsonPath content returning spades6 and spades7 in the Test Expression tab
jsonPath returns the CSV text of the content field; csv skips four rows (header, jack, ten, nine) and returns the second column of the rest.

Notes

  • Rows and columns count differently: headerRowsToSkip is a count of rows, columnIndex is zero-based.
  • Every row after the skipped ones needs at least columnIndex + 1 columns. One shorter row fails the whole expression, and no value is returned for the other rows either. The Test tab then says, for example: Column index 2 is out of range on row 2, which has 1 column (the first column is 0).
  • A headerRowsToSkip that isn't smaller than the number of rows fails too: The number of header rows to skip (20) is not smaller than the number of rows (12).
  • A file with several sections, such as a header, details, and footer, has rows of different widths. Cut out the section you need with regex first, and run csv on that, as in the walkthrough above.
  • For fixed-width records, use the Flat File Fixed Width plugin. When one column is all you need and nothing has to be nested, the Flat File CSV plugin does the job without a formula; see Choose a plugin.

Next Steps