Crates.io | flatten-json-object |
lib.rs | flatten-json-object |
version | 0.6.1 |
source | src |
created_at | 2022-04-05 21:52:57.313158 |
updated_at | 2022-04-16 23:07:32.671381 |
description | Tiny Rust library for flattening JSON objects |
homepage | |
repository | https://github.com/vtselfa/flatten-json-object |
max_upload_size | |
id | 562806 |
size | 29,126 |
Given a JSON object it produces another one with all the nested objects and arrays flattened. The string used to separate the concatenated keys, and the way the arrays are formatted can be configured.
""
and the JSON null
value can be used without problems and are preserved.In your Cargo.toml
[dependencies]
flatten-json-object = "0.6.1"
use flatten_json_object::ArrayFormatting;
use flatten_json_object::Flattener;
use serde_json::json;
let obj = json!({
"a": {
"b": [1, 2.0, "c", null, true, {}, []],
"" : "my_key_is_empty"
},
"" : "my_key_is_also_empty"
});
assert_eq!(
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)
.flatten(&obj)?,
json!({
"a.b[0]": 1,
"a.b[1]": 2.0,
"a.b[2]": "c",
"a.b[3]": null,
"a.b[4]": true,
"a.": "my_key_is_empty",
"": "my_key_is_also_empty",
})
);
A trivial example that reads JSON
from stdin
and outputs the converted flat JSON
to stdout
can be found in examples/from_stdin.rs.
To run it execute cargo run --example from_stdin
.