8 个版本
0.2.6 | 2024年4月12日 |
---|---|
0.2.5 | 2024年4月2日 |
0.2.4 | 2024年3月19日 |
0.1.0 | 2024年3月15日 |
#477 in 数据结构
59 每月下载量
21KB
460 行
smooth-json
此 crate 允许将 JSON 对象扁平化到可用于 Parquet、CSV 或其他数据格式的对象。
扁平化类似于 ElasticSearch 的 导入扁平化 或 VAST 的数据库和表格 集成 所需的内容。
特性
- 将 serde_json 的
Value
变体扁平化到适合与期望表格格式数据或列式数据格式应用程序一起使用的结构。 - 通过实例化一个
Flattener
并传递分隔符来传递自定义分隔符。 - 使用具有观点的扁平化格式,如果对象嵌套在数组中,则将值放置在数组中
示例
标准用法
use smooth_json;
use serde_json::json;
fn main() {
let flattener = smooth_json::Flattener::new();
let example = json!({
"name": "John Doe",
"age": 43,
"address": {
"street": "10 Downing Street",
"city": "London"
},
"phones": [
"+44 1234567",
"+44 2345678"
]
});
let flattened_example = flattener.flatten(&example);
println!("{}", flattened_example);
/*
{
"address.city": "London",
"address.street": "10 Downing Street",
"age": 43,
"name": "John Doe",
"phones": [
"+44 1234567",
"+44 2345678"
]
}
*/
}
自定义分隔符用法
use serde_json::json;
use smooth_json;
fn main() {
let flattener = smooth_json::Flattener{
separator: "$",
..Default::default()
};
let example = json!({
"a": {
"b": 1
}});
let flattened_example = flattener.flatten(&example);
println!("{}", flattened_example);
/*
{
"a$b": 1
}
*/
}
具有观点的扁平化
如果对象存在于数组中,扁平化后的结果将是在数组中的。
use serde_json::json;
use smooth_json;
fn main() {
let flattener = smooth_json::Flattener{
alt_array_flattening: true,
..Default::default()
};
let example = json!({
"a": [
["b", "c"],
{ "d": "e" },
["f", "g"],
[
{ "h": "i" },
{ "d": "j" },
],
["k", "l"],
]
});
let flattened_example = flattener.flatten(&example);
println!("{}", flattened_example);
/*
{
"a": ["b", "c", "f", "g", "k", "l"],
"a.d": ["e", "j"],
"a.h": ["i"],
}
*/
}
完全扁平的 JSON(保持数组位置在键中)
use serde_json::json;
use smooth_json;
fn main() {
let flattener = smooth_json::Flattener{
preserve_arrays: true,
..Default::default()
};
let example: Value = json!({
"a": [
"b",
["c", "d"],
]
});
let flattened_example = flattener.flatten(&example);
println!("{}", flattened_example);
/*
{
"a.0": "b",
"a.1.0": "c",
"a.1.1": "d"
}
*/
依赖项
~0.5–1MB
~20K SLoC