Lean 4 (Meta)programming Cookbook

How to Read JSONL files🔗

To read a JSONL file, we use IO.FS.lines to get an array of strings and then parse each non-empty line.

def readJsonlFile (path : System.FilePath) : IO (Array Json) := do let lines IO.FS.lines path let mut result := #[] for line in lines do let line := line.`String.trim` has been deprecated: Use `String.trimAscii` instead Note: The updated constant has a different type: String String.Slice instead of String Stringtrim if line.isEmpty then continue match Json.parse line with | .ok j => result := result.push j | .error err => throw <| IO.userError s!"Failed to parse line: {err}" return result