Serde deserializing a JSON top-level dictionary

I have JSON data like this

{
    "add0" : {
        "callcreates" : []
       
    },
    "add1" : {
        "callcreates" : []
    }
    //.... 254 other opcodes
}

And I would capture this in a Rust structure. At the top level I have what seems to be a hashmap in Rust. But note the JSON Is not something like

{"opcodes":  {"add1": ... "add2: ... }}

where the there is a placeholder for the top-level hashmap.

For the value of the hashmap, I could define a struct.

I have been able to use serde_json::Value to read in the entire structure, but then I don't know how to iterate over the top-level JSON keys.

Suggestions?

P.S. JSON serialization in Rust, part 2 - valve's seems to do what I want except it no longer works playpen

Have you tried just deserializing into a normal std::collections::HashMap? If that needs to be within a struct then you can either wrap it later, or use the #[serde(flatten)] annotation.

(playground)

1 Like

This is fantastic and does exactly what is needed. Many thanks.