# JSON3.jl struct read

**URL:** https://discourse.julialang.org/t/json3-jl-struct-read/80751
**Category:** Data
**Tags:** question, json3
**Created:** [May 9, 2022, 12:05pm UTC](https://discourse.julialang.org/t/json3-jl-struct-read/80751 "2022-05-09T12:05:31Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)
#### Post date: [May 9, 2022, 12:05pm UTC](https://discourse.julialang.org/t/json3-jl-struct-read/80751/1 "2022-05-09T12:05:31Z")

</div>

```julia
using JSON3

struct Foo
    foo::Int
    bar::String
end

JSON3.StructTypes.StructType(::Type{Foo}) = JSON3.StructTypes.Struct()

obj = Foo(1, "bar")

```

If if just have the pure struct it works fine:

```julia
julia> json = JSON3.write(obj)
"{\"foo\":1,\"bar\":\"bar\"}"

julia> JSON3.read(json, Foo)
Foo(1, "bar")

```

How can i construct the type if it is just a field of a dict?

```julia
julia> json = JSON3.write(Dict("a" => 1, "b" => obj))
"{\"b\":{\"foo\":1,\"bar\":\"bar\"},\"a\":1}"

julia> JSON3.read(json)
JSON3.Object{Base.CodeUnits{UInt8, String}, Vector{UInt64}} with 2 entries:
  :b => {…
  :a => 1

```

This can’t be it:

```julia
julia> JSON3.read(JSON3.write(JSON3.read(json)["b"]), Foo)
Foo(1, "bar")

```

---

<div class="post-metadata">

### Author: ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)
#### Post date: [May 9, 2022, 7:55pm UTC](https://discourse.julialang.org/t/json3-jl-struct-read/80751/2 "2022-05-09T19:55:58Z")

</div>

I can think of two options;

1. You could define a quick wrapper struct:

```julia
mutable struct Wrapper
    b::Foo
    Wrapper() = new()
end
StructTypes.StructType(::Type{Wrapper}) = StructTypes.Mutable()

JSON3.read(json, Wrapper)

```

1. The StructTypes.jl package has a utility function called `StructTypes.constructfrom(T, obj)`, that could be used in your case like:

```julia
foo = StructTypes.constructfrom(Foo, JSON3.read(json)["b"])

```

Basically, it allows using the `StructType` information for `T` to extract the properties from any `AbstractDict` provided, which `JSON3.Object` satisfies.
