4 个版本
0.1.3 | 2022 年 12 月 17 日 |
---|---|
0.1.2 | 2022 年 5 月 20 日 |
0.1.1 | 2022 年 4 月 16 日 |
0.1.0 | 2022 年 4 月 15 日 |
#1069 in 编码
每月 2,691 次下载
33KB
379 行
用于将 JSON 对象转换为 CSV 行的健壮 Rust 库
给定一个 JSON 对象数组或包含连续 JSON 对象的文件,它将生成一个 CSV 文件,每个 JSON 对象对应一行。为了将 JSON 对象转换为 CSV 行,该库“扁平化”对象,将它们转换为没有嵌套对象或数组的等效对象。用于扁平化对象的规则是可以配置的,但默认情况下,如下面的对象
{"a": {"b": [1,2,3]}}
被转换为扁平化的 JSON 对象
{
"a.b.0": 1,
"a.b.1": 2,
"a.b.2": 3
}
然后用于生成以下 CSV 输出
a.b.0,a.b.1,a.b.2
1,2,3
配置输出
此库依赖于 flatten-json-object
进行 JSON 对象扁平化以及 csv
进行 CSV 文件生成。如果您想调整输出外观,请查看它们的相应文档。
说明
- 对象的扁平化和 CSV 格式(例如,字段分隔符)可以配置。
- 输入中的每个顶层对象都将被转换为 CSV 行。
- 标题按字母顺序排序,并且是输入中所有扁平化对象的键的并集。
- 扁平化输入后的键冲突将被报告为错误,即如果两个对象的键应该是不同的,但在扁平化后看起来相同。例如,默认情况下,扁平化包含以下内容的文件将导致错误:
- 任何实例
{}
(当不是一个顶层对象时),[]
或Null
将导致空 CSV 字段。
从 Read
实现器读取的示例
use csv;
use flatten_json_object::ArrayFormatting;
use flatten_json_object::Flattener;
use json_objects_to_csv::Json2Csv;
use std::io::{Read, Write};
use std::str;
// Anything supported by the `Flattener` object is possible.
let flattener = Flattener::new()
.set_key_separator(".")
.set_array_formatting(ArrayFormatting::Surrounded{
start: "[".to_string(),
end: "]".to_string()
})
.set_preserve_empty_arrays(false)
.set_preserve_empty_objects(false);
// The output can be anything that implements `Write`. In this example we use a vector but
// this could be a `File`.
let mut output = Vec::<u8>::new();
// Anything that implements `Read`. Usually a file, but we will use a byte array in this example.
let input = r#"{"a": {"b": 1}} {"c": [2]} {"d": []} {"e": {}}"#.as_bytes();
// The CSV rows that we should get from this input and config. Note that since we are not
// preserving empty arrays or objects `d` and `e` are not part of the final headers.
// However, an empty row is generate for their object. If empty objects and arrays were
// preserved both `e` and `d` would be part of the headers, but their column would be empty.
let expected = ["a.b,c[0]", "1,", ",2", ",", ","];
// Here we can configure another field separator, like `;` or use any other CSV builder
// configuration.
let csv_writer = csv::WriterBuilder::new()
.delimiter(b',')
.from_writer(&mut output);
Json2Csv::new(flattener).convert_from_reader(input, csv_writer)?;
assert_eq!(str::from_utf8(&output)?, expected.join("\n") + "\n");
转换 JSON 对象切片的示例
use csv;
use flatten_json_object::ArrayFormatting;
use flatten_json_object::Flattener;
use json_objects_to_csv::Json2Csv;
use serde_json::json;
use std::str;
// We changed the array formatting and we preserve empty arrays and objects now, compared to
// the previous example.
let flattener = Flattener::new()
.set_key_separator(".")
.set_array_formatting(ArrayFormatting::Plain)
.set_preserve_empty_arrays(true)
.set_preserve_empty_objects(true);
// The output can be anything that implements `Write`. In this example we use a vector but
// this could be a `File`.
let mut output = Vec::<u8>::new();
let input = [
json!({"a": {"b": 1}}),
json!({"c": [2]}),
json!({"d": []}),
json!({"e": {}})
];
// This time the separator is `;`
let csv_writer = csv::WriterBuilder::new()
.delimiter(b';')
.from_writer(&mut output);
// The CSV rows that we should get from this input and config. We are preserving empty arrays
// and objects so `d` and `e` are part of the final headers. Since they are empty and no other
// object has those headers these columns have no value in any of the rows.
let expected = ["a.b;c.0;d;e", "1;;;", ";2;;", ";;;", ";;;"];
Json2Csv::new(flattener).convert_from_array(&input, csv_writer)?;
assert_eq!(str::from_utf8(&output)?, expected.join("\n") + "\n");
依赖关系
~3–12MB
~148K SLoC