Skip to content

Commit

Permalink
json: improved error handling for first_dict; added TODO to call se…
Browse files Browse the repository at this point in the history
…lect directly
  • Loading branch information
jqnatividad committed Jun 27, 2024
1 parent c76a282 commit 173a8ad
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/cmd/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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(","))
Expand Down

0 comments on commit 173a8ad

Please sign in to comment.