Serde_json how to read as singe entry?

Hi,
have a json file looks like:

ABC: 10
DEF: 20
GHI: 30

When try to read that with:

let ABC = json["ABC"].as_str().unwrap();
let DEF = json["DEF"].as_str().unwrap();
let GHI = json["GHI"].as_str().unwrap();

got error:

called Option::unwrap() on a None value

What did I wrong ?

This is not valid JSON though, maybe that's the problem.

1 Like

thanks - is there a way to read it anyway ?

Not with a json parser. First you'd have to actually know the data format then get or write the appropriate parser.

1 Like

sorry the json file is

{
"ABC": 10,
"DEF": 20,
"GHI": 30
}

Can you post your actual code and data with more context than just 2 lines? If that is your actual json and actual code, it should've worked.

1 Like

use reqwest::blocking::get;
use serde_json::{ Value, from_str };
use current_platform::{CURRENT_PLATFORM, COMPILED_ON};
use sys_locale::get_locale;
use std::path::Path;
use gettext_ng::Catalog;
use std::fs::File;

fn main(){
    const VERSION: &str = env!("CARGO_PKG_VERSION");
    const NAME: &str = env!("CARGO_PKG_NAME");
    let current_locale = get_locale().unwrap_or_else(|| String::from("en-US"));
    let filename = format!("{}.mo", current_locale);

    let file_path = format!("{current_locale}.mo");
    let path = Path::new(&file_path);
    if path.exists() {
        println!("LanguageFile: {file_path} exists!");
        let f = File::open(filename).expect("could not open the catalog");
        let catalog = Catalog::parse(f).expect("could not parse the catalog");


    println!("Platform: {} Program was compiled on {}", CURRENT_PLATFORM, COMPILED_ON);
    println!("The locale is {}\n", current_locale);
    println!("{} v{}\n", NAME, VERSION);
    println!("{}", catalog.gettext("\nInfo Checker"));

    let url = format!("https://myserver.mydomain.tld/info.json");
    let response = get(url).unwrap();
    let response_text = response.text().unwrap();
    let json: Value = from_str(&response_text).expect("JSON was not well-formatted");

    
    let ABC = json["ABC"].as_str().unwrap();
    let DEF = json["DEF"].as_str().unwrap();
    let GHI = json["GHI"].as_str().unwrap();


   println!("\n{} ABC\n",  ABC);
   println!("\n{} DEF\n",  DEF);
   println!("\n{} GHI\n",  GHI);

} else {
    println!("File: {file_path} does not exist! Program abort");
}

}

Your issue is probably that 10 isn't a string. It's a int.

1 Like

thanks
but use ".as_str" at unwrap
should be string

as_str tries to convert a json value into a string, giving you an Option<String>. Since the value is an integer, you get a panic when you call unwrap.

thanks

if I change the values to: a, d, g
the same error

{
"ABC": a,
"DEF": d,
"GHI": g
}

That isn't valid JSON.

1 Like

thanks, is this a valid json ?

{
"ABC": 10.1,
"DEF": 20.1,
"GHI": 30.1
}

Yes, but now your values are floats.

1 Like

Ok thanks - when I cange the code to:

    let ABC = json["ABC"].as_f64().unwrap();
    let DEF = json["DEF"].as_f64().unwrap();
    let GHI = json["GHI"].as_f64().unwrap();

have the same error:

called Option::unwrap() on a None value

That has to mean the data you're reading isn't the one you think you should and the one you showed us.

1 Like

sorry my fault had typo.... now it works!
thanks @all

1 Like
use reqwest::blocking::get;
use serde_json::{ Value, from_str };
use current_platform::{CURRENT_PLATFORM, COMPILED_ON};
use sys_locale::get_locale;
use std::path::Path;
use gettext_ng::Catalog;
use std::fs::File;

fn main(){
    const VERSION: &str = env!("CARGO_PKG_VERSION");
    const NAME: &str = env!("CARGO_PKG_NAME");
    let current_locale = get_locale().unwrap_or_else(|| String::from("en-US"));
    let filename = format!("{}.mo", current_locale);

    let file_path = format!("{current_locale}.mo");
    let path = Path::new(&file_path);
    if path.exists() {
        println!("LanguageFile: {file_path} exists!");
        let f = File::open(filename).expect("could not open the catalog");
        let catalog = Catalog::parse(f).expect("could not parse the catalog");


    println!("Platform: {} Program was compiled on {}", CURRENT_PLATFORM, COMPILED_ON);
    println!("The locale is {}\n", current_locale);
    println!("{} v{}\n", NAME, VERSION);
    println!("{}", catalog.gettext("\nInfo Checker"));

    let url = format!("https://myserver.mydomain.tld/info.json");
    let response = get(url).unwrap();
    let response_text = response.text().unwrap();
    let json: Value = from_str(&response_text).expect("JSON was not well-formatted");

    
    let ABC = json["ABC"].as_f64().unwrap();
    let DEF = json["DEF"].as_f64().unwrap();
    let GHI = json["GHI"].as_f64().unwrap();


   println!("\n{} ABC\n",  ABC);
   println!("\n{} DEF\n",  DEF);
   println!("\n{} GHI\n",  GHI);

} else {
    println!("File: {file_path} does not exist! Program abort");
}

}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.