CSV and JSON with external crates
Quick answer
CSV represents tabular records and JSON represents structured values such as objects and arrays. Rust's standard library does not provide complete CSV or JSON parsers. Use dedicated crates to handle quoting, escapes and malformed data correctly.
Use reviewed dependencies and validate decoded values
The existing candidate lab pins csv 1.3.1, serde 1.0.219 with derive, and serde_json 1.0.140. These pins are not proof of resolved dependency compatibility. Prepare a reviewed local Cargo project with those exact dependencies and a lockfile; obtain dependencies only with approval. CSV readers recognize quoted commas; Serde derives trait implementations for typed records. Bound inputs and check domain rules after decoding.
// Cargo.toml: csv = "=1.3.1"fn main() -> Result<(), Box<dyn std::error::Error>> { let data = b"name,score\n\"Ada, Jr.\",9\n"; let mut reader = csv::Reader::from_reader(&data[..]); for row in reader.records() { let row = row?; println!("{}: {}", &row[0], &row[1]); } Ok(())}Manual local Rust/Cargo candidate; not executed. Expected behavior:
Ada, Jr.: 9The header is consumed by default and the quoted name stays one field.
// Cargo.toml: serde = { version = "=1.0.219", features = ["derive"] }// serde_json = "=1.0.140"#[derive(serde::Deserialize)]struct Item { count: u16 }fn main() -> Result<(), Box<dyn std::error::Error>> { let item: Item = serde_json::from_str(r#"{"count":101}"#)?; println!("accepted: {}", (1..=100).contains(&item.count)); Ok(())}Manual local Rust/Cargo candidate; not executed. Expected behavior:
accepted: falseDecoding into u16 succeeds; the domain check still rejects 101.
Common mistake
Splitting CSV on every comma breaks quoted fields; treating JSON as plain text loses structure.
Try it yourself
Change the JSON count to 5, then try a string count and explain the different outcomes.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
CSV and JSON with external crates — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A CSV name contains a comma inside quotes. How should the record be decoded?
CSV and JSON with external crates — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust // Cargo.toml: csv = "=1.3.1" fn main() -> Result<(), Box<dyn std::error::Error>> { let data = b"name,score\n\"Ada, Jr.\",9\n"; let mut reader = csv::Reader::from_reader(&data[..]); for row in reader.records() { let row = row?; println!("{}: {}", &row[0], &row[1]); } Ok(()) } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
CSV and JSON with external crates — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust // Cargo.toml: serde = { version = "=1.0.219", features = ["derive"] } // serde_json = "=1.0.140" #[derive(serde::Deserialize)] struct Item { count: u16 } fn main() -> Result<(), Box<dyn std::error::Error>> { let item: Item = serde_json::from_str(r#"{"count":101}"#)?; println!("accepted: {}", (1..=100).contains(&item.count)); Ok(()) } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.