Rust Serde deserialization of a json response doesn't work for a particular field

Viewed 253

I am trying to gather data from a json response in Rust, using serde, and I am running into a problem.

First, here is the relevant part of the json response

 {
    "data": {
         "id": "ws-eezeioA1J2dW3cwK",
         "relationships": {
                    "organization": {
                        "data": {
                            "id": "DevOps",
                            "type": "organizations"
                        }
                    },
                    "current-run": {
                        "data": {
                            "id": "run-XEFXdZj6MKwpoNJx",
                            "type": "runs"
                        },
                        "links": {
                            "related": "/api/v2/runs/run-XEFXdZj6MKwpoNJx"
                        }
                    },
            }
        }
}

and here are my structs in rust

#[derive(Deserialize, Debug)]
struct Workspace {
    data: WorkspaceData,
}

#[derive(Deserialize, Debug)]
struct WorkspaceData {
    id: String,
    relationships: WsRelashionships,
}
#[derive(Deserialize, Debug)]
struct WsRelashionships {
    #[serde(alias = "current-run")]
    current_run: Data,
}
struct Data {
    id: String,
}

finally, the line of code that read the json reponse

workspace_data = serde_json::from_str(response.text().unwrap().as_str())?;

Basically, I want to extract the "id" field under data and "current-run", but I keep getting this error for the id field under "current-run"

Err( Serialization( Error("missing field id", line: 1, column: 1515), ), )

It's like it's not seeing the "id" field under current run. Any idea what is wrong in the code?

1 Answers

Oh, geez, I found it 5 mins after posting this. I am missing one layer in my structs.

#[derive(Deserialize, Debug)]
struct Workspace {
    data: WorkspaceData,
}

#[derive(Deserialize, Debug)]
struct WorkspaceData {
    id: String,
    relationships: WsRelashionships,
}
#[derive(Deserialize, Debug)]
struct WsRelashionships {
    #[serde(alias = "current-run")]
    current_run: CurrentRunData,
}

#[derive(Deserialize, Debug)]
struct CurrentRunData {
    data: Data,
}

struct Data {
    id: String,
}
Related