# JSON string to composite type

**URL:** <https://discourse.julialang.org/t/json-string-to-composite-type/56112>\
**Category:** New to Julia\
**Tags:** json\
**Created:** [February 26, 2021, 6:59pm UTC](https://discourse.julialang.org/t/json-string-to-composite-type/56112 "2021-02-26T18:59:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vladflore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vladflore/32/18139_2.png) [@vladflore](https://discourse.julialang.org/u/vladflore)\
**Post date:** [February 26, 2021, 6:59pm UTC](https://discourse.julialang.org/t/json-string-to-composite-type/56112/1 "2021-02-26T18:59:43Z")

</div>

Hi,  
I was trying to consume a rest API, specifically to make a GET request and transform the response into a custom composite type. Unfortunately, I am missing something, because I am getting errors I cannot quite understand. This might be a trivial thing, but as I do not have much experience with it, I would appreciate any hint or tip on how to get it working.

The response data looks like this (an array of objects):

```julia
json_string = """
[
  {
    "id": "1",
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "Sincere@april.biz",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    "phone": "1-770-736-8031 x56442",
    "website": "hildegard.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    }
  }
]
"""

```

I have the `struct`s defined:

```julia
struct User 
    id::String
    name::String
    username::String
    email::String
    address::Address
    phone::String
    website::String
    company::Company
end

struct Address
    street::String
    suite::String
    city::String
    zipcode::String
    geo::Geo
end

struct Geo
    lat::String
    lng::String
end

struct Company
    name::String
    catchPhrase::String
    bs::String
end

StructTypes.StructType(::Type{User}) = StructTypes.Struct()
StructTypes.StructType(::Type{Address}) = StructTypes.Struct()
StructTypes.StructType(::Type{Geo}) = StructTypes.Struct()
StructTypes.StructType(::Type{Company}) = StructTypes.Struct()

```

When I am doing this:  
`JSON3.read(json_string, JSON3.Array{User})`

I get an error saying:  
`ERROR: LoadError: UndefVarError: Address not defined`

What am I missing here?

Thanks,  
Vlad

---

<div class="post-metadata">

**Author:** ![cchderrick](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@cchderrick](https://discourse.julialang.org/u/cchderrick)\
**Post date:** [February 26, 2021, 7:42pm UTC](https://discourse.julialang.org/t/json-string-to-composite-type/56112/2 "2021-02-26T19:42:29Z")

</div>

Can you try move you `User` struct definition after `Address` and `Company`, also `Address` after `Geo`?

---

<div class="post-metadata">

**Author:** ![vladflore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vladflore/32/18139_2.png) [@vladflore](https://discourse.julialang.org/u/vladflore)\
**Post date:** [February 26, 2021, 8:03pm UTC](https://discourse.julialang.org/t/json-string-to-composite-type/56112/3 "2021-02-26T20:03:43Z")

</div>

Thank! It works now! I didn’t expect that to be the problem…but thanks again!
