From bd19a624469df652f24ee37b65056535ff665769 Mon Sep 17 00:00:00 2001 From: Andrew Lamb Date: Mon, 5 Feb 2024 06:01:51 -0500 Subject: [PATCH] Add example of converting RecordBatches to JSON objects (#5364) * Add example of converting RecordBatches to JSON objects * twea * Update docs * Use from_reader and simplify example --- arrow-json/src/writer.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/arrow-json/src/writer.rs b/arrow-json/src/writer.rs index dd77328cb7b5..acca702907ea 100644 --- a/arrow-json/src/writer.rs +++ b/arrow-json/src/writer.rs @@ -74,7 +74,36 @@ //! [`LineDelimitedWriter`] and [`ArrayWriter`] will omit writing keys with null values. //! In order to explicitly write null values for keys, configure a custom [`Writer`] by //! using a [`WriterBuilder`] to construct a [`Writer`]. - +//! +//! ## Writing to [serde_json] JSON Objects +//! +//! To serialize [`RecordBatch`]es into an array of +//! [JSON](https://docs.serde.rs/serde_json/) objects you can reparse the resulting JSON string. +//! Note that this is less efficient than using the `Writer` API. +//! +//! ``` +//! # use std::sync::Arc; +//! # use arrow_array::{Int32Array, RecordBatch}; +//! # 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 json bytes (string) +//! let buf = Vec::new(); +//! let mut writer = arrow_json::ArrayWriter::new(buf); +//! writer.write_batches(&vec![&batch]).unwrap(); +//! writer.finish().unwrap(); +//! let json_data = writer.into_inner(); +//! +//! // Parse the string using serde_json +//! use serde_json::{Map, Value}; +//! let json_rows: Vec> = serde_json::from_reader(json_data.as_slice()).unwrap(); +//! assert_eq!( +//! serde_json::Value::Object(json_rows[1].clone()), +//! serde_json::json!({"a": 2}), +//! ); +//! ``` mod encoder; use std::iter;