# How to make JSON nest Class or Structure

**URL:** <https://forums.kodeco.com/t/how-to-make-json-nest-class-or-structure/51375>\
**Category:** Server Side Swift with Vapor\
**Created:** [October 1, 2018, 5:26am UTC](https://forums.kodeco.com/t/how-to-make-json-nest-class-or-structure/51375 "2018-10-01T05:26:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![miyabi](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/miyabi/32/81494_2.png) [@miyabi](https://forums.kodeco.com/u/miyabi)\
**Post date:** [October 1, 2018, 5:26am UTC](https://forums.kodeco.com/t/how-to-make-json-nest-class-or-structure/51375/1 "2018-10-01T05:26:18Z")

</div>

Hello. I will post to the forum for the first time.  
I’m trying to make my own Vapor APP after finishing the book and video tutorial now.

I encountered some things I could not do in it. It is trying to create a class or struct json is nesting in JSON and save it in postgre.

In addition to suffering from errors such as the format of Future not matching, even if you define an empty array in Class and try to save it, it is an error such as different Element.

How do you manage a model that JSON has nested within JSON?  
Please let me know if you do not mind.  
I created MyAcronymModel and JSON format is

MyAcronym.swift  
import Vapor  
import Foundation  
import FluentPostgreSQL

```
final class MyAcronym:Codable,Content,PostgreSQLModel,Parameter {
    var user:User.Public
    var acronyms:[Acronym]
    var id:Int?
}

extension MyAcronym:Migration {
    
}

```

MyAcronymController.swift  
`import Foundation`  
import Vapor  
import FluentPostgreSQL  
import Authentication

```
struct MyAcronymControler:RouteCollection {
    func boot(router: Router) throws {
        let myAcronymRoute = router.grouped("api/myacronym")
        let tokenAuthMiddleware = User.tokenAuthMiddleware()
        let guardAuthMiddleware = User.guardAuthMiddleware()
        let tokenAuthGroup = myAcronymRoute.grouped(tokenAuthMiddleware, guardAuthMiddleware)
        /*going to make createMyAcronymRoute
         */
    }
    
    func createMyAcronyms(_ req:Request,UserData:User.Public) -> Future<MyAcronym>{
          return try req.parameters.next(User.self).map(to:[Acronym].self){ user in
            try user.acronyms.query(on: req).all()
            let data = myAcronymData(user: UserData, acronyms: queryData)
      }
    }
}

struct myAcronymData {
    var user:User.Public
    var acronyms:[Acronym]
}

```

Multiple errors will be issued at func createMyAcronyms. The rest is the same as the tutorial. How did anyone solve if someone had a similar challenge?

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [October 29, 2018, 11:23pm UTC](https://forums.kodeco.com/t/how-to-make-json-nest-class-or-structure/51375/2 "2018-10-29T23:23:05Z")

</div>

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![0xtim](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/0xtim/32/111111_2.png) [@0xtim](https://forums.kodeco.com/u/0xtim)\
**Post date:** [November 20, 2018, 1:11pm UTC](https://forums.kodeco.com/t/how-to-make-json-nest-class-or-structure/51375/3 "2018-11-20T13:11:20Z")

</div>

Ok so models with models inside…

The answer is it’s complicated! You can’t just set a property like you have, because it doesn’t work with the database.

There is a way to store models like that, but it means you can’t just query the `Acronym` table, since it doesn’t exist. If you want to return something like that in JSON, you need to create a _new_ `Content` type and populate it manually. It’s known as the N+1 query and is something that Fluent doesn’t do very well.

If you join the Discord forum, there’s lots of solutions on there for it.
