Module arrow_json::writer
source · Expand description
JSON Writer
This JSON writer converts Arrow RecordBatches into arrays of
JSON objects or JSON formatted byte streams.
Writing JSON Objects
To serialize RecordBatches into array of
JSON objects, use
record_batches_to_json_rows:
let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);
let a = Int32Array::from(vec![1, 2, 3]);
let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a)]).unwrap();
let json_rows = arrow_json::writer::record_batches_to_json_rows(&[&batch]).unwrap();
assert_eq!(
serde_json::Value::Object(json_rows[1].clone()),
serde_json::json!({"a": 2}),
);Writing JSON formatted byte streams
To serialize RecordBatches into line-delimited JSON bytes, use
LineDelimitedWriter:
let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);
let a = Int32Array::from(vec![1, 2, 3]);
let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a)]).unwrap();
// Write the record batch out as JSON
let buf = Vec::new();
let mut writer = arrow_json::LineDelimitedWriter::new(buf);
writer.write_batches(&vec![&batch]).unwrap();
writer.finish().unwrap();
// Get the underlying buffer back,
let buf = writer.into_inner();
assert_eq!(r#"{"a":1}
{"a":2}
{"a":3}
"#, String::from_utf8(buf).unwrap())To serialize RecordBatches into a well formed JSON array, use
ArrayWriter:
use arrow_schema::{DataType, Field, Schema};
let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);
let a = Int32Array::from(vec![1, 2, 3]);
let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a)]).unwrap();
// Write the record batch out as a JSON array
let buf = Vec::new();
let mut writer = arrow_json::ArrayWriter::new(buf);
writer.write_batches(&vec![&batch]).unwrap();
writer.finish().unwrap();
// Get the underlying buffer back,
let buf = writer.into_inner();
assert_eq!(r#"[{"a":1},{"a":2},{"a":3}]"#, String::from_utf8(buf).unwrap())Structs
- Produces JSON output as a single JSON array. For example
- Produces JSON output with one record per line. For example
- A JSON writer which serializes
RecordBatches to a stream ofu8encoded JSON objects. See the module level documentation for detailed usage and examples. The specific format of the stream is controlled by theJsonFormattype parameter.
Traits
- This trait defines how to format a sequence of JSON objects to a byte stream.
Functions
Type Definitions
- A JSON writer which serializes
RecordBatches to JSON arrays - A JSON writer which serializes
RecordBatches to newline delimited JSON objects