# Unmarshalling to a Julia type when type has Union in the Attribute values?

**URL:** <https://discourse.julialang.org/t/unmarshalling-to-a-julia-type-when-type-has-union-in-the-attribute-values/46563>\
**Category:** New to Julia\
**Tags:** json\
**Created:** [September 13, 2020, 8:40pm UTC](https://discourse.julialang.org/t/unmarshalling-to-a-julia-type-when-type-has-union-in-the-attribute-values/46563 "2020-09-13T20:40:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DataPsycho](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datapsycho/32/16891_2.png) [@DataPsycho](https://discourse.julialang.org/u/DataPsycho)\
**Post date:** [September 13, 2020, 8:40pm UTC](https://discourse.julialang.org/t/unmarshalling-to-a-julia-type-when-type-has-union-in-the-attribute-values/46563/1 "2020-09-13T20:40:23Z")

</div>

Hi,  
I am trying to unmarshal a json to a Struct type where there is Union in type attributes but it does not work. Is there a way to do that?

That exercise works:

```julia
using Unmarshal

input = "{ \"id\": 25073877, \"id_str\": \"25073877\"}"

struct UserX
    id::Int64
    id_str::String
end

Unmarshal.unmarshal(UserX, JSON.parse(input))

```

But Having Union in the struct Marshalling is not working:

```julia
struct UserY
    id::Union{Int64, Nothing}
    id_str::Union{String, Nothing}
end

Unmarshal.unmarshal(UserY, JSON.parse(input))

```

It through the following error:

```julia
ERROR: MethodError: unmarshal(::Type{Union{Nothing, String}}, ::String, ::Bool, ::Int64) is ambiguous. Candidates:
  unmarshal(DT::Type, parsedJson::String, verbose::Bool, verboseLvl::Int64) in Unmarshal at /home/datapsycho/.julia/packages/Unmarshal/G72dP/src/Unmarshal.jl:38
  unmarshal(::Type{Union{Nothing, T}}, x, verbose::Bool, verboseLvl::Int64) where T in Unmarshal at /home/datapsycho/.julia/packages/Unmarshal/G72dP/src/Unmarshal.jl:232
Possible fix, define
  unmarshal(::Type{Union{Nothing, T}}, ::String, ::Bool, ::Int64) where T

```

Is there any other way I can do marshal?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 13, 2020, 9:06pm UTC](https://discourse.julialang.org/t/unmarshalling-to-a-julia-type-when-type-has-union-in-the-attribute-values/46563/2 "2020-09-13T21:06:59Z")

</div>

Defining the function as proposed is doing what you want:

```julia
julia> import Unmarshal.unmarshal

julia> unmarshal(::Type{Union{T,Nothing}}, x :: String, verbose :: Bool = false, verboseLvl :: Int = 0) where T = unmarshal(T, x, verbose, verboseLvl)
unmarshal (generic function with 69 methods)

julia> Unmarshal.unmarshal(UserY, JSON.parse(input))
UserY(25073877, "25073877")

```

But I think that this would be worth opening an issue at… I see, you already did:  
[https://github.com/lwabeke/Unmarshal.jl/issues/31](https://github.com/lwabeke/Unmarshal.jl/issues/31)

Well done.
