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.1.0"
```rust use flattenjsonobject::ArrayFormatting; use flattenjsonobject::Flattener; use serde_json::json;
let obj = json!({ "a": { "b": [1, 2.0, "c", null, true, {}, []], "" : "mykeyisempty" }, "" : "mykeyisalsoempty" }); asserteq!( Flattener::new() .setkeyseparator(".") .setarrayformatting(ArrayFormatting::Surrounded { start: "[".tostring(), end: "]".tostring() }) .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.": "mykeyisempty", "": "mykeyisalso_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.