Skip to main content

json_to_csv

Function json_to_csv 

Source
pub fn json_to_csv(json: &str) -> Result<String, Box<dyn Error>>
Expand description

Converts a JSON array of objects into a CSV string.

The header row is the union of all keys across all objects. Missing keys become empty cells. Nested objects or arrays cause an error to prevent silent corruption of round-trips.

§Errors

Returns an error if the input is not an array of objects, contains nested values, or if writing fails.

§Examples

use sheeter_engine::json_to_csv;

let json = r#"[{"name":"Alice","age":"30"},{"name":"Bob","age":"25"}]"#;
assert_eq!(json_to_csv(json).unwrap(), "name,age\nAlice,30\nBob,25\n");