JSON string to composite type

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):

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 structs defined:

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

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

1 Like

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