0

I have JSON where a key has an array of objects and I'm only interested in one of the strings within that:

{
    "actors": [{
        "name": "Bob",
        "foo": "bar"
    }, {
        "name": "Jim",
        "foo": "baz"
    }],
    "fruits": [{
        "name": "Pineapple",
        "foo": "bar"
    }, {
        "name": "Apple",
        "foo": "baz"
    }]
}

I have

use serde::Deserialize; // 1.0.110

#[derive(Deserialize)]
pub struct Name {
    pub name: String,
}

#[derive(Deserialize)]
pub struct Data {
    pub actors: Vec<Name>,
    pub fruits: Vec<Name>,
}

How can I flatten that so I can have Vec<String> rather than Vec<Name>? e.g. I want to end up with

actors: ["Bob", "Jim"],
fruits: ["Pineapple", "Apple"],
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
SuperWig
  • 155
  • 3
  • 14
  • You want to replace `Vec` with `Vec` but still able to use the same format (e.g. `"actors" : [{"name": "Bob", ...}]`) rather than flattanned (e.g. `"actors" : ["Bob", ..]`)? – Kitsu Jul 06 '20 at 10:52
  • I just want it flattened i.e. `"actors": ["Bob", "Jim",...]` – SuperWig Jul 06 '20 at 10:57
  • May you also clarify that in your answer? – Kitsu Jul 06 '20 at 11:02
  • Thanks, I've clarified the OP. What you answered seems to be the wrong way round. – SuperWig Jul 06 '20 at 11:17
  • Ok, then it still not clear what do you mean by "end up with" section. You want to transform json? Or just print the data in the specified format? – Kitsu Jul 06 '20 at 11:46
  • I want `Data`'s `Vec` fields to be `Vec` that's just the name of the inner json object. – SuperWig Jul 06 '20 at 11:52
  • 1
    [The duplicate applied to your situation](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=607f6e1f17d6c520540b884202d0b9ad) – Shepmaster Jul 06 '20 at 14:09

0 Answers0