Reading CSV and JSON data in OCaml
To work in OCaml on real data (spreadsheet exports, open data…), Basthon offers two libraries: csv and yojson.
ocaml
#require "csv";;
#require "yojson";;
let rows = Csv.Rows.load ~has_header:true "students.csv";;
List.map (fun r -> Csv.Row.find r "name") rows;;
let json = Yojson.Safe.from_string {|{"name": "Ada", "marks": [15, 18]}|};;
Yojson.Safe.Util.(json |> member "marks" |> to_list);;A file opened in Basthon (or attached to the notebook) is read directly, and a file produced is downloaded with Basthon.download. The example notebook computes means from a CSV file, writes a ranking, builds and analyses JSON, then converts the CSV into JSON.
