Merge Dictionaries with values of type Vector

Hi everyone,

I try to merge two dictionaries and one of them contains a vector value. Unfortunately, all the merging methods used return the same error:

body = Dict(
    :scope => "deposited"
  )
# an optional parameter for a request to an API
statusParameter = Dict(
    :status => ["pending"]
  )

# body[:status] = ["pending"]
# setindex!(body, ["pending"], :status)
merge!(body, statusParameter)

Cannot convert an object of type Vector{String} to an object of type String

I would like to achieve something like:

Dict{Symbol, Any} with 2 entries:
  :status => ["pending"]
  :scope  => "deposited"

Does anyone know how to do it?

Best,
Josselin.

You need to start with a Dict that has the right type, so you could do:

julia> blank = Dict{Symbol, Any}()
Dict{Symbol, Any}()

julia> merge!(blank, body, statusParameter)
Dict{Symbol, Any} with 2 entries:
  :status => ["pending"]
  :scope  => "deposited"

Or you can merge out-of-place, and Julia will find an appropriate type covering both cases (here it chooses Any):

julia> merge(body, statusParameter)
Dict{Symbol, Any} with 2 entries:
  :status => ["pending"]
  :scope  => "deposited"
2 Likes

@Satvik and @gdalle, Thanks to both of you! I thought merge() and merge!() worked the same way, with no type needed…
Best,