Rust
Rust
QuickBooks - Parse the JSON of a Customer Balance Detail Report
See more CSV Examples
This example is to show how to parse the JSON of a particular Quickbooks report. The techniques shown here may help in parsing similar reports.The JSON to be parsed is available at Sample Quickbooks Customer Balance Detail Report JSON
Chilkat Rust Downloads
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = chilkat::Http::new();
// Get the JSON we'll be parsing..
let Ok(json_str) = http.quick_get_str("https://www.chilkatsoft.com/exampleData/qb_customer_balance_detail_report_2.json") else {
println!("{}", http.last_error_text());
return;
};
let json = chilkat::JsonObject::new();
let _ = json.load(&json_str);
// As an alternative to manually writing code, use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
// Let's parse the JSON into a CSV, and then save to a CSV file.
let csv = chilkat::Csv::new();
csv.set_has_column_names(true);
// Set the column names of the CSV.
let mut num_columns = json.size_of_array("Columns.Column");
if num_columns < 0 {
println!("Unable to get column names");
return;
}
let mut i = 0;
while i < num_columns {
json.set_i(i);
let _ = csv.set_column_name(i, &json.string_of("Columns.Column[i].ColTitle").unwrap_or_default());
i = i + 1;
}
// Let's get the rows.
// We'll ignore the Header and Summary, and just get the data.
let mut row = 0;
let num_rows = json.size_of_array("Rows.Row[0].Rows.Row");
if num_rows < 0 {
println!("Unable to get data rows");
return;
}
while row < num_rows {
json.set_i(row);
num_columns = json.size_of_array("Rows.Row[0].Rows.Row[i].ColData");
let mut col = 0;
while col < num_columns {
json.set_j(col);
let _ = csv.set_cell(row, col, &json.string_of("Rows.Row[0].Rows.Row[i].ColData[j].value").unwrap_or_default());
col = col + 1;
}
row = row + 1;
}
// Show the CSV
println!("{}", csv.save_to_string().unwrap_or_default());
// Save to a CSV file
let _ = csv.save_file("qa_output/customerDetailReport.csv").is_ok();