From 173a8adabee2c41f5b1e08efef332742537168a9 Mon Sep 17 00:00:00 2001 From: Joel Natividad <1980690+jqnatividad@users.noreply.github.com> Date: Thu, 27 Jun 2024 08:17:09 -0400 Subject: [PATCH] `json`: improved error handling for first_dict; added TODO to call select directly --- src/cmd/json.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/cmd/json.rs b/src/cmd/json.rs index d1bb2bac4..2acdcbe39 100644 --- a/src/cmd/json.rs +++ b/src/cmd/json.rs @@ -120,14 +120,11 @@ pub fn run(argv: &[&str]) -> CliResult<()> { if value.is_null() { return fail_clierror!("No JSON data found."); } - // safety: value is not null let first_dict = value .as_array() - .unwrap() - .first() - .unwrap() - .as_object() - .unwrap(); + .and_then(|arr| arr.first()) + .and_then(|val| val.as_object()) + .ok_or_else(|| CliError::Other("Expected an array of objects in JSON".to_string()))?; let mut headers: Vec<&str> = Vec::new(); for key in first_dict.keys() { headers.push(key.as_str()); @@ -138,6 +135,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> { Json2Csv::new(flattener).convert_from_array(values, csv_writer)?; // Use qsv select to reorder headers to first dict's keys order + // TODO: call select helpers directly instead of using a child process let mut select_child = std::process::Command::new(env::current_exe()?) .arg("select") .arg(headers.join(","))