JSON files
Read & write
JSON
Reading a JSON file should look familiar:
JsonReader
· Available on feature json
use polars::prelude::*;
let mut file = std::fs::File::open("docs/data/path.json").unwrap();
let df = JsonReader::new(&mut file).finish().unwrap();
Newline Delimited JSON
JSON objects that are delimited by newlines can be read into polars in a much more performant way than standard json.
JsonLineReader
· Available on feature json
let mut file = std::fs::File::open("docs/data/path.json").unwrap();
let df = JsonLineReader::new(&mut file).finish().unwrap();
Write
JsonWriter
· JsonWriter
· Available on feature json
let mut df = df!(
"foo" => &[1, 2, 3],
"bar" => &[None, Some("bak"), Some("baz")],
)
.unwrap();
let mut file = std::fs::File::create("docs/data/path.json").unwrap();
// json
JsonWriter::new(&mut file)
.with_json_format(JsonFormat::Json)
.finish(&mut df)
.unwrap();
// ndjson
JsonWriter::new(&mut file)
.with_json_format(JsonFormat::JsonLines)
.finish(&mut df)
.unwrap();
Scan
Polars
allows you to scan a JSON input only for newline delimited json. Scanning delays the actual parsing of the
file and instead returns a lazy computation holder called a LazyFrame
.
LazyJsonLineReader
· Available on feature json
let df = LazyJsonLineReader::new("docs/data/path.json".to_string()).finish().unwrap();