Skip to main content

json_to_yaml

Function json_to_yaml 

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

Converts a JSON array of objects into a YAML sequence of mappings.

This is the inverse of yaml_to_json. The output is a YAML 1.2 document where each object becomes a mapping in a sequence. Key order is preserved. Nested objects or arrays cause an error for the same reason as CSV: they cannot round-trip losslessly through a flat format.

§Errors

Returns an error if the JSON is not an array of objects, contains nested values, or if YAML serialization fails.

§Examples

use sheeter_engine::json_to_yaml;

let json = r#"[{"name":"Alice","age":"30"}]"#;
let yaml = json_to_yaml(json).unwrap();
assert!(yaml.contains("name: Alice"));