Deserialize JSON array of structs without element types using serde

I am trying to figure out how to deserialize some json I have to deal with using serde. Can it be done with attributes? I have a structure that matches the contents, but the standard serialize has the structure name.
This is the JSON I want to load:

[
	{
		"User": "user",
		"Password": "xx",
	},
	{
		"User": "root",
		"Password": "pwd",
	}
]

I have a struct with deserialize defined and tried deserializing Vec<UserStruct>
This is what serde is expecting with my current attempts:

[
	UserStruct { 
		User: "u1", 
		Password: "u1p", 
	}, 
	UserStruct { 
		User: "u2", 
		Password: "u2p", 
	}
]

I have a struct with deserialize defined and tried deserializing Vec<UserStruct>

any suggestions welcome.
Thanks
johnt

mind sharing those current attempts?

For comparison, ⟶ this expects the format you want (apart from trailing commas)

Also make sure to wrap Rust code in

```
code here
```

and plain text (no highlighting) in

```plain
my text here
```

or

```text
my text here
```

For example Rust-highlighting works reasonably for JSON, too, or you could use javascript hightlighting. See this pinned post.

[
    {
        "User": "user",
        "Password": "xx"
    },
    {
        "User": "root",
        "Password": "pwd"
    }
]

Thanks, I will dig a bit deeper, and post the results. This looks similar to what I was doing, so it may be something stupid on my part.
johnt

So it turns out I was misinterpreting the problem at the end of a long day.
As you indicated above it does what I wanted.
It wasn't that there isn't the UserStruct, it is that the data I am being passed has a higher level wrapper around it. A combination of misreading the error message and looking at the wrong debug output confused me.
So I just needed to handle the top level and it worked fine.
Thank you again for the help, and the helpful edit that made the posting much more readable. I format better next time.
thanks
johnt

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.