use serde_json::{json, Value};
pub fn unwind_json(wound_json: Value, unwind_on: &String) -> Vec<Value> {
let mut output = Vec::new();
let sub_array = wound_json.get(unwind_on).unwrap().as_array().unwrap();
let mut wound_json_minus_unwind_key = wound_json.as_object().unwrap().clone();
wound_json_minus_unwind_key.remove(unwind_on);
for item in sub_array {
let mut new_json = wound_json_minus_unwind_key.clone();
new_json.insert(unwind_on.clone(), item.clone());
output.push(Value::from(new_json));
}
output
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn simple_test() {
assert_eq!(unwind_json(json!({"a": 1, "b": [1,2]}), &String::from("b")),
*json!([{"a": 1, "b": 1}, {"a": 1, "b": 2}]).as_array().unwrap())
}
}