# Reading a nested JSON file into a predefined type with JSON3

**URL:** <https://discourse.julialang.org/t/reading-a-nested-json-file-into-a-predefined-type-with-json3/89011>\
**Category:** New to Julia\
**Created:** [October 20, 2022, 1:42pm UTC](https://discourse.julialang.org/t/reading-a-nested-json-file-into-a-predefined-type-with-json3/89011 "2022-10-20T13:42:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_J](https://avatars.discourse-cdn.com/v4/letter/r/58956e/32.png) [@Robert\_J](https://discourse.julialang.org/u/Robert_J)\
**Post date:** [October 20, 2022, 1:42pm UTC](https://discourse.julialang.org/t/reading-a-nested-json-file-into-a-predefined-type-with-json3/89011/1 "2022-10-20T13:42:23Z")

</div>

Lets say with have this JSON file :

```julia
{
    "A": "String",
    "B": [
        {
            "B1": "String",
            "B2": "String",
        },
        {
            "B1": "String",
            "B2": "String",
        },
        {
            "B1": "String",
            "B2": "String",
        }
    ]
}

```

The B key has a nested list of sub keys. The problem I’m running into is I can’t make a predefined type to read the whole thing into it and return (as in this case) 3 entries. The nested files I define like this:

```julia
struct nested
B1::String
B2::String
end

```

I ran into problem defining the whole thing in the next step.

```julia
Struct Json
A::String
B:: ?
end

```

JSON3.jl documentation uses a `StructTypes.CustomStruct` to wrap different structs but I can’t get it to work. How should I define the ‘B’ parameter in the Struct Json? (The goal of the whole thing is to reduce allocation of `JSON3.read`)

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [October 20, 2022, 2:14pm UTC](https://discourse.julialang.org/t/reading-a-nested-json-file-into-a-predefined-type-with-json3/89011/2 "2022-10-20T14:14:37Z")

</div>

Looks like `B::Vector{nested}`, although I would capitalize it to `Nested`

---

<div class="post-metadata">

**Author:** ![Robert\_J](https://avatars.discourse-cdn.com/v4/letter/r/58956e/32.png) [@Robert\_J](https://discourse.julialang.org/u/Robert_J)\
**Post date:** [October 20, 2022, 6:08pm UTC](https://discourse.julialang.org/t/reading-a-nested-json-file-into-a-predefined-type-with-json3/89011/3 "2022-10-20T18:08:21Z")

</div>

Thanks for the reply. That was much simpler than I thought.

Interestingly, Im seeing performance regression. Imma post in performance section to find out why.
