Module polars_io::json[][src]

This is supported 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;

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":100000000000000, "b":0.6, "c":false, "d":"text"}"#;
let file = Cursor::new(basic_json);
let df = JsonReader::new(file)
.infer_schema(Some(3))
.with_batch_size(3)
.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

JSON Reader

JSON file reader builder

A two-dimensional dataset with a number of columns (Array) and rows and defined Schema.

Type Definitions