Crates.io | json-unflattening |
lib.rs | json-unflattening |
version | 0.1.4 |
source | src |
created_at | 2023-12-06 10:32:11.452116 |
updated_at | 2024-09-18 14:56:57.590418 |
description | A Rust library for flattening and unflattening JSON structures. |
homepage | |
repository | https://github.com/Cybersecurity-LINKS/json-unflattening |
max_upload_size | |
id | 1059871 |
size | 37,525 |
A Rust library for flattening and unflattening JSON structures. Uses serde_json
for JSON serialization and deserialization.
Add this library to your Cargo.toml
:
[dependencies]
json-unflattening = "0.1.4"
use json_unflattening::{flatten, unflatten};
fn main() {
let input_json = json!({
"name": {
"first": "John",
"last": "Doe"
},
"age": 30,
"city": "New York",
"hobbies": ["Reading", "Hiking", "Gaming"]
});
let flattened_json = flatten(&input_json).unwrap();
println!("Flattened JSON: {:#}", serde_json::to_string_pretty(&flattened_json).unwrap());
let unflattened_json = unflatten(&flattened_json).unwrap();
println!("Unflattened JSON: {:#}", unflattened_json);
}
{
"name": {
"first": "John",
"last": "Doe"
},
"age": 30,
"city": "New York",
"hobbies": ["Reading", "Hiking", "Gaming"]
}
{
"name.first": "John",
"name.last": "Doe",
"age": 30,
"city": "New York",
"hobbies[0]": "Reading",
"hobbies[1]": "Hiking",
"hobbies[2]": "Gaming"
}
Flatten Object Properties:
"name"
object properties using dot notation: "name.first"
and "name.last"
."age"
and "city"
directly without modification.Result:
{
"name.first": "John",
"name.last": "Doe",
"age": 30,
"city": "New York"
}
Flatten Array Elements:
"hobbies"
by appending indices to each element: "hobbies[0]"
, "hobbies[1]"
, and "hobbies[2]"
.Result:
{
"name.first": "John",
"name.last": "Doe",
"age": 30,
"city": "New York",
"hobbies[0]": "Reading",
"hobbies[1]": "Hiking",
"hobbies[2]": "Gaming"
}