Available on crate feature
json
only.Expand description
§(De)serialize JSON files.
§Read JSON to a DataFrame
§Example
use polars_core::prelude::*;
use polars_io::prelude::*;
use std::io::Cursor;
use std::num::NonZeroUsize;
let basic_json = r#"{"a":1, "b":2.0, "c":false, "d":"4"}
{"a":-10, "b":-3.5, "c":true, "d":"4"}
{"a":2, "b":0.6, "c":false, "d":"text"}
{"a":1, "b":2.0, "c":false, "d":"4"}
{"a":7, "b":-3.5, "c":true, "d":"4"}
{"a":1, "b":0.6, "c":false, "d":"text"}
{"a":1, "b":2.0, "c":false, "d":"4"}
{"a":5, "b":-3.5, "c":true, "d":"4"}
{"a":1, "b":0.6, "c":false, "d":"text"}
{"a":1, "b":2.0, "c":false, "d":"4"}
{"a":1, "b":-3.5, "c":true, "d":"4"}
{"a":1, "b":0.6, "c":false, "d":"text"}"#;
let file = Cursor::new(basic_json);
let df = JsonReader::new(file)
.with_json_format(JsonFormat::JsonLines)
.infer_schema_len(NonZeroUsize::new(3))
.with_batch_size(NonZeroUsize::new(3).unwrap())
.finish()
.unwrap();
println!("{:?}", df);
Outputs:
+-----+--------+-------+--------+
| a | b | c | d |
| --- | --- | --- | --- |
| i64 | f64 | bool | str |
+=====+========+=======+========+
| 1 | 2 | false | "4" |
+-----+--------+-------+--------+
| -10 | -3.5e0 | true | "4" |
+-----+--------+-------+--------+
| 2 | 0.6 | false | "text" |
+-----+--------+-------+--------+
| 1 | 2 | false | "4" |
+-----+--------+-------+--------+
| 7 | -3.5e0 | true | "4" |
+-----+--------+-------+--------+
| 1 | 0.6 | false | "text" |
+-----+--------+-------+--------+
| 1 | 2 | false | "4" |
+-----+--------+-------+--------+
| 5 | -3.5e0 | true | "4" |
+-----+--------+-------+--------+
| 1 | 0.6 | false | "text" |
+-----+--------+-------+--------+
| 1 | 2 | false | "4" |
+-----+--------+-------+--------+
Structs§
- Reads JSON in one of the formats in
JsonFormat
into a DataFrame. - Writes a DataFrame to JSON.
Enums§
- The format to use to write the DataFrame to JSON:
Json
(a JSON array) orJsonLines
(each row output on a separate line). In either case, each row is serialized as a JSON object whose keys are the column names and whose values are the row’s corresponding values.