Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Can't find solution for rust error " Error("EOF while parsing an object", line: 1, column: 1)"

Ask Question fn main() { // Open and read the file lines let path = r"C:\Users\egete\Desktop\projects\cdeneme\rust\mysqljson\src\info.json"; let file = File::open(path).expect("Unable to read file"); let reader = BufReader::new(file); let lines: Vec<String> = reader.lines().map(|l| l.expect("Could not parse line")).collect(); // Connect to your my-sql database let url = Opts::from_url("mysql://root:Egetelli99@localhost:3306/jsonmysql").unwrap(); let pool = Pool::new(url).unwrap(); let mut conn = pool.get_conn().unwrap(); // Loop through the lines for line in lines { // Parse the line into a JSON object serde_json::Value let v: Value = serde_json::from_str(&line).expect("Unable to parse"); // Since we want to save the streaming services of the said show, we need to // loop an array inside the JSON file. let l = v["watchAvailability"][0]["directUrls"].as_array().unwrap().len(); for n in 0..l { let streaming_url = v["watchAvailability"][0]["directUrls"][n].as_str().clone(); match streaming_url { Some(url) => { // Display the streaming url. I have to do this to remove the Some(url) warning. // Unused variables in Rust emits warnings println!("{:?}", url); // Create a vector (array of object). // This provides you the ability to process multiple objects upon saving let shows = vec![ Show { title: v["title"].as_str().as_deref().unwrap_or("Error").to_string(), show_poster: v["posterPath"].as_str().as_deref().unwrap_or("Error").to_string(), show_url: v["watchAvailability"][0]["directUrls"][n].as_str().as_deref().unwrap_or("Error").to_string(), //Execute an insert query conn.exec_batch( r"INSERT INTO `shows` (`title`, `show_poster`, `show_url`) VALUES (:title, :show_poster, :show_url)", shows.iter().map(|s| params! { "title" => s.title.clone(), "show_poster" => s.show_poster.clone(), "show_url" => s.show_url.clone(), ).unwrap_err(); _ => println!("Error"),

What is the problem there ? This error shows up : thread 'main' panicked at 'Unable to parse: Error("EOF while parsing an object", line: 1, column: 1)', src\main.rs:30:52 note: run with RUST_BACKTRACE=1 environment variable to display a backtrace error: process didn't exit successfully: target\debug\mysqljson.exe (exit code: 101)

Why are you trying to parse a json file line by line? It looks like there's an empty line in info.json is empty. isaactfa Aug 17, 2022 at 14:05 It's a common format for streamable JSON where each line is an individual record. jsonlines.org But yeah, I agree it's probably just an empty line. PitaJ Aug 17, 2022 at 15:18

You are reading a file. But serde can't figure out how to serialize that data on the file if does not exactly matches a json format.

Also, you may be aware that the OS system you're using, 'cause reading lines could making you fall of due to the \r\n vs \n differences.

I have run into the same issue. In my case, I try to read JSON file, and the problem was that it was empty, I've just added {} to the file, and this fixed my issue. Hope it will help someone

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question . Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers .