# How to convert a Dict to a Struct

**URL:** https://discourse.julialang.org/t/how-to-convert-a-dict-to-a-struct/116916
**Category:** General Usage
**Tags:** question
**Created:** [July 11, 2024, 3:24pm UTC](https://discourse.julialang.org/t/how-to-convert-a-dict-to-a-struct/116916 "2024-07-11T15:24:24Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [July 11, 2024, 3:24pm UTC](https://discourse.julialang.org/t/how-to-convert-a-dict-to-a-struct/116916/1 "2024-07-11T15:24:25Z")

</div>

How can I convert a dict to a struct?

I have the following code:

```julia
set_dict=Dict{String, Any}("log_level" => 1.0, "area" => 20.0, "solver" => "IDA")

mutable struct Settings
    log_level::Int64
    area::Float64
    solver::String
end

function set_struct(set_dict)
    set = Settings(0, 0, "")
    for (key, value) in set_dict
        print(key, ": "); println(value)
        # set field of the struct with the name key to the corresponding value
    end
    set
end

set_struct(set_dict)

```

But how can I access the fields of a struct using a string variable?

The dictionary will usually have less entries than the struct has fields.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [July 11, 2024, 3:31pm UTC](https://discourse.julialang.org/t/how-to-convert-a-dict-to-a-struct/116916/2 "2024-07-11T15:31:49Z")

</div>

This works:

```julia
set_dict=Dict{String, Any}("log_level" => 1.0, "area" => 20.0, "solver" => "IDA")

mutable struct Settings
    log_level::Int64
    area::Float64
    solver::String
end

function set_struct(set_dict)
    set = Settings(0, 0, "")
    for (key, value) in set_dict
        # set field of the struct with the name key to the corresponding value
        setproperty!(set, Symbol(key), value)
    end
    set
end

set_struct(set_dict)

```

---

<div class="post-metadata">

### Author: ![JonasWickman](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JonasWickman](https://discourse.julialang.org/u/JonasWickman)
#### Post date: [July 11, 2024, 6:49pm UTC](https://discourse.julialang.org/t/how-to-convert-a-dict-to-a-struct/116916/4 "2024-07-11T18:49:33Z")

</div>

You already solved it, but here is a way to make it a bit more compact if you like:

```julia
@kwdef struct Settings
        log_level::Int64
        area::Float64
        solver::String
end

set_dict = Dict{String, Any}("log_level" => 1.0, "area" => 20.0, "solver" => "IDA")
set_struct = Settings(; (Symbol.(keys(set_dict)) .=> values(set_dict))... )

```

This way, you can also avoid having a mutable `Settings`, which may or may not be handy.

Note that if the original `Dict` used symbols instead of strings, conversion to a struct would be trivial:

```julia
sym_dict = Dict{Symbol, Any}(:log_level => 1.0, :area => 20.0, :solver => "IDA")
set_struct = Settings(; sym_dict...)

```

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [July 11, 2024, 7:14pm UTC](https://discourse.julialang.org/t/how-to-convert-a-dict-to-a-struct/116916/5 "2024-07-11T19:14:13Z")

</div>

Even without the `@kwdef` in plain Julia:

```julia
Settings(getindex.(Ref(set_dict),String.(fieldnames(Settings)))...)

```

gives:

```julia
Settings(1, 20.0, "IDA")

```

---

<div class="post-metadata">

### Author: ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)
#### Post date: [July 11, 2024, 9:21pm UTC](https://discourse.julialang.org/t/how-to-convert-a-dict-to-a-struct/116916/6 "2024-07-11T21:21:09Z")

</div>

Yes, but if

> [@ufechner7](#):
>
> The dictionary will usually have less entries than the struct has fields.

as in the original question, it get’s a bit longer, but the following is very general and also works for immutable structs:

```julia
struct Settings
    log_level::Int64
    area::Float64
    solver::String
end

function createme(prototype, dict)
    ST = typeof(prototype)
    fields = fieldnames(ST)
    ST(get.(Ref(dict), string.(fields), getfield.(Ref(prototype), fields))...)
end

proto = Settings(0, 0, "")
createme(proto, Dict("log_level" => 1.0, "area" => 20.0))

```
