Skip to content

data.formats

read and write JSON and CSV text

Turns text into values and values back into text. Parsing a JSON or CSV string gives lists and maps you can work with, and writing does the reverse.

Tags: parsing, formatting

  • from_json read JSON text into zil values: objects become maps, arrays lists, and null nil
  • to_json v as JSON text
  • from_csv read CSV (comma-separated) text
  • to_csv CSV text from a table, a list of maps, or a list of lists
from_json(s: str) -> any

read JSON text into zil values: objects become maps, arrays lists, and null nil

1
"\{\"a\": [1, 2.5, null]}".from_json{a: [1, 2.5, nil]}
Example outputCtrl+Enter to run

See also to_json, parse

to_json(v: any) -> str

v as JSON text

Values JSON has no type for, like quantities, fractions and dates, become strings

1
{a: [1, nil], b: 5 km}.to_json"{\"a\":[1,null],\"b\":\"5 km\"}"
Example outputCtrl+Enter to run

See also from_json

from_csv(s: str, header: bool = true) -> table|list

read CSV (comma-separated) text

The first row is taken as column names and the result is a table. Pass false if there’s no such row, to get a list of rows. Numbers become numbers and empty cells nil

1
"name,n\nx,1".from_csvtable([{name: "x", n: 1}])
2
"1,2\n3,4".from_csv(false)[[1, 2], [3, 4]]
Example outputCtrl+Enter to run

See also to_csv, table

to_csv(rows: table|list) -> str

CSV text from a table, a list of maps, or a list of lists

Cells with commas or quotes are quoted

1
[{a: 1, b: "x,y"}].to_csv
a,b 1,"x,y"
2
[[1, 2], [3, 4]].to_csv
1,2 3,4
Example outputCtrl+Enter to run

See also from_csv

1
# read JSON and add up a list in it
2
"\{\"a\": [1, 2]}".from_json.a.sum3
3
# read CSV and add up a column
4
"name,n\nx,1\ny,2".from_csv.n.sum3
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.