Why do I always get a "trailing characters" error when trying to parse data with serde_json?

Viewed 3666

I have a server that returns requests in a JSON format. When trying to parse the data I always get "trailing characters" error. This happens only when getting the JSON from postman

let type_of_request = parsed_request[1];
let content_of_msg: Vec<&str> = msg_from_client.split("\r\n\r\n").collect();

println!("{}", content_of_msg[1]); 
// Will print "{"username":"user","password":"password","email":"dwadwad"}"

let res: serde_json::Value = serde_json::from_str(content_of_msg[1]).unwrap();

println!("The username is: {}", res["username"]);

when getting the data from postman this happens:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error("trailing characters", line: 1, column: 60)', src\libcore\result.rs:997:5

but when having the string inside Rust:

let j = "{\"username\":\"user\",\"password\":\"password\",\"email\":\"dwadwad\"}";

let res: serde_json::Value = serde_json::from_str(j).unwrap();

println!("The username is: {}", res["username"]);

it works like a charm:

The username is: "user"

EDIT: Apparently as I read the message into a buffer and turned it into a string it saved all the NULL characters the buffer had which are of course the trailing characters.

1 Answers

Looking at the serde json code, one finds the following comment above the relevant ErrorCode enum element:

/// JSON has non-whitespace trailing characters after the value.
TrailingCharacters,

So as the error code implies, you've got some trailing character which is not whitespace. In your snippet, you say:

println!("{}", content_of_msg[1]); 
// Will print "{"username":"user","password":"password","email":"dwadwad"}"

If you literally copy and pasted the printed output here, I'd note that I wouldn't expect the output to be wrapped in the leading and trailing quotation marks. Did you include these yourself or were they part of what was printed? If they were printed, I suspect that's the source of your problem.

Edit:

In fact, I can nearly recreate this using a raw string with leading/trailing quotation marks in Rust:

extern crate serde_json;

#[cfg(test)]
mod tests {
    #[test]
    fn test_serde() {
        let s =
            r#""{"username":"user","password":"password","email":"dwadwad"}""#;
        println!("{}", s);
        let _res: serde_json::Value = serde_json::from_str(s).unwrap();
    }
}

Running it via cargo test yields:

test tests::test_serde ... FAILED

failures:

---- tests::test_serde stdout ----
"{"username":"user","password":"password","email":"dwadwad"}"
thread 'tests::test_serde' panicked at 'called `Result::unwrap()` on an `Err` value: Error("trailing characters", line: 1, column: 4)', src/libcore/result.rs:997:5
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.


failures:
    tests::test_serde

Note that my printed output also includes leading/trailing quotation marks and I also get a TrailingCharacter error, albeit at a different column.

Edit 2:

Based on your comment that you've added the wrapping quotations yourself, you've got a known good string (the one you've defined in Rust), and one which you believe should match it but doesn't (the one from Postman).

This is a data problem and so we should examine the data. You can adapt the below code to check the good string against the other:

#[test]
fn test_str_comp() {
    // known good string we'll compare against                                                                                                                                             
    let good =
        r#"{"username":"user","password":"password","email":"dwadwad"}"#;
    // lengthened string, additional characters                                                                                                                                            
    // also n and a in username are transposed                                                                                                                                             
    let bad =
        r#"{"useranme":"user","password":"password","email":"dwadwad"}abc"#;
    let good_size = good.chars().count();
    let bad_size = bad.chars().count();
    for (idx, (c1, c2)) in (0..)
        .zip(good.chars().zip(bad.chars()))
        .filter(|(_, (c1, c2))| c1 != c2)
    {
        println!(
            "Strings differ at index {}: (good: `{}`, bad: `{}`)",
            idx, c1, c2
        );
    }
    if good_size < bad_size {
        let trailing = bad.chars().skip(good_size);
        println!(
            "bad string contains extra characters: `{}`",
            trailing.collect::<String>()
        );
    } else if good_size > bad_size {
        let trailing = good.chars().skip(bad_size);
        println!(
            "good string contains extra characters: `{}`",
            trailing.collect::<String>()
        );
    }

    assert!(false);
}

For my example, this yields the failure:

test tests::test_str_comp ... FAILED

failures:

---- tests::test_str_comp stdout ----
Strings differ at index 6: (good: `n`, bad: `a`)
Strings differ at index 7: (good: `a`, bad: `n`)
bad string contains extra characters: `abc`
thread 'tests::test_str_comp' panicked at 'assertion failed: false', src/lib.rs:52:9
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.


failures:
    tests::test_str_comp
Related