You are not logged in.

#1 2022-06-11 01:54:57

Debasish Patra
Member
Registered: 2014-03-06
Posts: 63

Rust : Queries on json_serde::Value

Hi Guys,
I am noticing that json_serde::Value is not able to convert a string to json, if String dont have doubel quotes around them.

e.g.

use serde_json::{Result, Value};
fn main() -> Result<()>{
      let raw_text = r##"{
          "url" : https://www.google.com
      }"##;

      println!("{}",raw_text);

      let v: Value = serde_json::from_str(raw_text)?;
      println!("{}", v["url"]);
      Ok(())
}

Output :

json git:master ❯❯❯ cargo run                                                                                                                                                                                                                ✭
   Compiling json v0.1.0 (/home/d/Documents/RustScripts/json)
    Finished dev [unoptimized + debuginfo] target(s) in 0.21s
     Running `target/debug/json`
{
          "url" : https://www.google.com
      }
Error: Error("expected value", line: 2, column: 19)

Any tips to handle this scenario will be useful.

Either I can add double quotes around the String where its missing or I need to find a way to deserialize the json.

Thanks smile


Keep Calm, And Enjoy Life smile

Offline

#2 2022-06-13 11:53:21

lmn
Member
Registered: 2021-05-09
Posts: 60

Re: Rust : Queries on json_serde::Value

The Problem is that you are trying to parse invalid json and the library yelling at you for doing so. A string in json is required to be in double quotes.

Either I can add double quotes around the String where its missing or I need to find a way to deserialize the json.

If your original data set does not have the right quoting you need to fix up your data set first. Is there a reason why you cant add the quotation marks?

Edit: just to prevent any misunderstandings, the string in question is the https://www.google.com

Last edited by lmn (2022-06-13 12:11:52)

Offline

#3 2022-06-13 19:09:36

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 16,418

Re: Rust : Queries on json_serde::Value

@Debasish Patra

You accidentally hit report instead of reply:

Debasish Patra wrote:

Thanks, even I was wondering the same. Why only url does not have quotes around them. It looked like a bug on the server, which has been corrected. Thanks for the explanation. I will close this . smile

Please mark the topic as [SOLVED] by editing the thread title.

Offline

Board footer

Powered by FluxBB