# Create Parameters object from a dictionary

**URL:** <https://discourse.julialang.org/t/create-parameters-object-from-a-dictionary/85912>\
**Category:** Tooling\
**Tags:** question, package, parametersjl\
**Created:** [August 18, 2022, 9:05am UTC](https://discourse.julialang.org/t/create-parameters-object-from-a-dictionary/85912 "2022-08-18T09:05:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [August 18, 2022, 9:05am UTC](https://discourse.julialang.org/t/create-parameters-object-from-a-dictionary/85912/1 "2022-08-18T09:05:17Z")

</div>

I have two dictionaries:

```nohighlight
d1 = Dict(:name => "Fido")
d2 = Dict(:name => "Spot", :breed => :poodle)

```

Which I’d like to use to create two

```nohighlight
using Parameters
@with_kw mutable struct Dog
    name::String
    breed::Symbol = :husky
end

```

objects.

I presumed the splat operator would achieve this as it’s equivalent would in python, but this does not work

```nohighlight
julia> Dog(d1...)
ERROR: MethodError: no method matching Dog(::Pair{Symbol, String})
Closest candidates are:
  Dog(::Any, ::Any) at ~/.julia/packages/Parameters/MK0O4/src/Parameters.jl:505
  Dog(; name, breed) at ~/.julia/packages/Parameters/MK0O4/src/Parameters.jl:493
  Dog(::Dog; kws...) at ~/.julia/packages/Parameters/MK0O4/src/Parameters.jl:569
  ...
Stacktrace:
 [1] top-level scope
   @ REPL[35]:1

julia> Dog(d2...)
ERROR: MethodError: Cannot `convert` an object of type Pair{Symbol, Any} to an object of type String
Closest candidates are:
  convert(::Type{String}, ::String) at essentials.jl:218
  convert(::Type{T}, ::T) where T<:AbstractString at strings/basic.jl:231
  convert(::Type{T}, ::AbstractString) where T<:AbstractString at strings/basic.jl:232
  ...
Stacktrace:
 [1] Dog(name::Pair{Symbol, Any}, breed::Pair{Symbol, Any})
   @ Main ~/.julia/packages/Parameters/MK0O4/src/Parameters.jl:505
 [2] top-level scope
   @ REPL[36]:1

```

How can I easily construct parameters objects from Dict?

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [August 18, 2022, 9:37am UTC](https://discourse.julialang.org/t/create-parameters-object-from-a-dictionary/85912/2 "2022-08-18T09:37:03Z")

</div>

If your dictionaries can be `NamedTuple`s instead

```julia
julia> d1 = (; name = "Fido")
(name = "Fido",)

julia> d2 = (; name = "Spot", breed = :poodle)
(name = "Spot", breed = :poodle)

```

Then you can do

```julia
julia> Dog(; d1...)
Dog
  name: String "Fido"
  breed: Symbol husky

julia> Dog(; d2...)
Dog
  name: String "Spot"
  breed: Symbol poodle

```

---

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [August 18, 2022, 9:45am UTC](https://discourse.julialang.org/t/create-parameters-object-from-a-dictionary/85912/3 "2022-08-18T09:45:53Z")

</div>

Not without an explicit conversion which I’d rather avoid.

My data is stored in json files which I’m loading into dictionaries, I can get symbol keys using [`()->DefaultDict{String,Any}(Missing)`](https://github.com/JuliaIO/JSON.jl#documentation). However, I’m unaware of how to get `JSON.jl` to return `NamedTuple` as opposed to dicts.

Loading `.json` `dict` data into a `Parameters.jl` struct would seem like a pretty common thing to do, so I presume I’m missing something. Pythons `pydantic` provides [`parse_file`](https://pydantic-docs.helpmanual.io/usage/models/#model-properties) which loads a json file and instantiate the applicable object.

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [August 18, 2022, 11:13am UTC](https://discourse.julialang.org/t/create-parameters-object-from-a-dictionary/85912/4 "2022-08-18T11:13:16Z")

</div>

JSON3.jl is generally recommended over JSON.jl nowadays, and it supports both [reading JSON into an existing type](https://quinnj.github.io/JSON3.jl/stable/#Read-JSON-into-a-type) and [creating types automatically based on the given JSON string](https://quinnj.github.io/JSON3.jl/stable/#Generated-Types).

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [August 18, 2022, 12:35pm UTC](https://discourse.julialang.org/t/create-parameters-object-from-a-dictionary/85912/5 "2022-08-18T12:35:19Z")

</div>

```julia
d = Dict([:name => "x"])
Dog(; d...)

```

Note the semicolon that makes `d` into keyword args. No need to convert the `Dict` to named tuples.

---

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [August 18, 2022, 12:55pm UTC](https://discourse.julialang.org/t/create-parameters-object-from-a-dictionary/85912/6 "2022-08-18T12:55:22Z")

</div>

Oh dear, it was that easy. I didn’t realise you had to specify that you’re using kwargs, I presumed passing a kwarg implicitly meant use kwargs.
