I would like to know if I am missing a pattern in JSON destructuring using serde-json
:
I am trying to parse a String-represented JSON into a struct but my struct only parse a part of the JSON. How would you do it?
A more precise example
The String
-represented JSON to parse :
{
fieldA : SomeValue,
fieldB : [TheArrayOfValuesIWant, WithPrettyMuch, TheSameTypeOfObjectsIn],
… // many other fields omitted
}
With fieldB
being the field I want to parse and the other field that I want to discard.
1 Like
You can deserialize into a struct containing only fieldB. Other fields will be ignored efficiently.
3 Likes
Hyeonu
June 24, 2019, 2:32pm
3
By default, serde-json discards excess fields of JSON object. So, this just enough:
#[derive(Deserialize)]
struct Data {
#[serde(rename = "fieldB")]
elems: Vec<Elem>,
}
let data: Data = serde_json::from_str(json).unwrap();
5 Likes
Thank you all for your replies ! @dtolnay @Hyeonu
I wish I could figure those small snippets myself : where could I have found this in the documentation ?
system
Closed
September 27, 2019, 11:20am
6
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.