find the creation date of a json file in rust

main.rs
use serde_json::Value;
use std::fs::metadata;

fn main() {
    let file_path = "example.json";
    let metadata = metadata(file_path).unwrap();
    let created = metadata.created().unwrap();
    let json_file: Value = serde_json::from_reader(std::fs::File::open(file_path).unwrap()).unwrap();
    println!("Creation Date: {:?}", created);
    println!("Json File Content: {}", json_file);
}
391 chars
12 lines

In this Rust code block, we start by importing the necessary libraries: serde_json for parsing JSON formatted data and std::fs along with metadata for getting metadata about the file. We then proceed with the following steps:

  • Specify the path of the file to be read (here it is "example.json").
  • Call metadata() function on file-path which will give us the metadata information about the file.
  • Call created() function on the metadata object, which will give us the date and time of the file creation.
  • Deserialize the JSON file content into Value struct by calling serde_json::from_reader() function.
  • Finally, we print the creation date of the file and the content of the JSON file.

related categories

gistlibby LogSnag