# Format json for return in a HasMany Smart Relationship

**URL:** <https://community.forestadmin.com/t/format-json-for-return-in-a-hasmany-smart-relationship/2544>\
**Category:** Help me!\
**Created:** [May 24, 2021, 9:52am UTC](https://community.forestadmin.com/t/format-json-for-return-in-a-hasmany-smart-relationship/2544 "2021-05-24T09:52:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benjamin](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/benjamin/32/2127_2.png) [@Benjamin](https://community.forestadmin.com/u/Benjamin)\
**Post date:** [May 24, 2021, 9:52am UTC](https://community.forestadmin.com/t/format-json-for-return-in-a-hasmany-smart-relationship/2544/1 "2021-05-24T09:52:56Z")

</div>

Hi everybody,

I would like to retrieve a user’s bank card list from the /user/{id}/cards route of my API. I was very successful in creating the smart relationship like this:

```javascript
router.get('/user/:userId/relationships/cards', async (request, response, next) => {
  let requestOuranos = await axios.get(process.env.API_APP_URL + '/' + process.env.API_VERSION + '/user/' + request.params.userId + '/cards', {
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer ' + process.env.API_SECRET
    },
    responseType: 'json'
  }).then((result) => {
    return {
      status: result.status,
      data: result.data
    };
  }).catch((error) => {
    return {
      status: error.response.status,
      data: error.response.data
    };
  });
});

```

I get a json format like this :

```javascript
[
  {
    id: 'card_1InRlOGhSnPh1ys0y5En3GrT',
    name: 'Ma CB',
    default: true,
    last4: '4242',
    expireMonth: 4,
    expireYear: 2024
  },
  {
    id: 'card_1InRpPGhSnPh1ys0XUtDkZaI',
    name: 'Mon AMEX',
    default: false,
    last4: '0005',
    expireMonth: 5,
    expireYear: 2045
  },
  {
    id: 'card_1InRoHGhSnPh1ys0mJxwAGwy',
    name: 'Ma CB Pro',
    default: false,
    last4: '4444',
    expireMonth: 4,
    expireYear: 2024
  }
]

```

I tried to return the json a little bit like this :

```javascript
return response.send(json);

```

Obviously this does not work :’)

Can someone say me how to format and simply return this json ?  
Thank’s

---

<div class="post-metadata">

**Author:** ![morganperre](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/morganperre/32/1296_2.png) [@morganperre](https://community.forestadmin.com/u/morganperre)\
**Post date:** [May 24, 2021, 10:33am UTC](https://community.forestadmin.com/t/format-json-for-return-in-a-hasmany-smart-relationship/2544/2 "2021-05-24T10:33:51Z")

</div>

Hi @Benjamin,

As you want to display a **has many relationships** you are on the right path. You need to serialise your data, in order to display them on the front. [Here’s some documentation the subject.](https://docs.forestadmin.com/documentation/reference-guide/collections/serializing-your-records)

You need to create a smart collection for the purpose of serialising your data. For example in your case:

```javascript
const { collection } = require('forest-express-sequelize');

collection('cards', {
  isSearchable: false,
  fields: [
  {
    field: 'id',
    type: 'String',
  }, {
    field: 'name',
    type: 'String',
    isFilterable: true,
  }, {
    field: 'default',
    type: 'Boolean',
  }, {
    field: 'last4',
    type: 'String',
  }, {
    field: 'expireMonth',
    type: 'Number',
  }, {
    field: 'expireYear',
    type: 'Number',
  }
  ],
});

```

Then you will be able to do the serialisation.

```javascript
// You need to import RecordSerializer from forest-express-sequelize
const { RecordSerializer } = require('forest-express-sequelize');

...

   // Create a RecordSerializer for the collection cards
   const serializer = new RecordSerializer({ name: 'cards' });

   // Seralize the data retrieve from a RecordSerializer for the collection cards
   const serializedRecords = await serializer.serialize(requestOuranos.data);

   // Finally send the serializedRecords to the client
   response.send(serializedRecords);

...

```

Let me know if it help. 🙂  
Best regards,  
Morgan

---

<div class="post-metadata">

**Author:** ![Benjamin](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/benjamin/32/2127_2.png) [@Benjamin](https://community.forestadmin.com/u/Benjamin)\
**Post date:** [May 24, 2021, 11:46am UTC](https://community.forestadmin.com/t/format-json-for-return-in-a-hasmany-smart-relationship/2544/3 "2021-05-24T11:46:29Z")

</div>

Hi Morgan,  
Thank you for your help.

Now I can get some data but not the one I want 😕

As you can see in this photo :

 ![Screenshot 2021-05-24 at 13.41.25](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/8/894153b7f0df9eee89c3f548555923d98c78b964.png)

I get the ID of the cards, but not the other information. And I don’t know why it shows me the user fields when I don’t need them.

---

<div class="post-metadata">

**Author:** ![morganperre](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/morganperre/32/1296_2.png) [@morganperre](https://community.forestadmin.com/u/morganperre)\
**Post date:** [May 24, 2021, 11:52am UTC](https://community.forestadmin.com/t/format-json-for-return-in-a-hasmany-smart-relationship/2544/4 "2021-05-24T11:52:54Z")

</div>

Hi again,

I think there a problem with the `.forestadmin-schema.json` synchronisation. Have you restarted your agent ?

Can you give the your project name ? I will take a closer look.  
Also, can you share me the implementation of the cards smart collection ?

Regards,  
Morgan

---

<div class="post-metadata">

**Author:** ![Benjamin](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/benjamin/32/2127_2.png) [@Benjamin](https://community.forestadmin.com/u/Benjamin)\
**Post date:** [May 24, 2021, 3:07pm UTC](https://community.forestadmin.com/t/format-json-for-return-in-a-hasmany-smart-relationship/2544/5 "2021-05-24T15:07:40Z")

</div>

Yes i have restart my agent, I also deleted my .forestadmin-schema.json but without successful.  
My project name is : [dev-v2]Alfred  
This is my cards smart collection :

```javascript
const { collection } = require('forest-express-sequelize');

collection('cards', {
    isSearchable: false,
    actions: [],
    fields: [
        {
            field: 'id',
            type: 'String',
        }, {
            field: 'name',
            type: 'String',
            isFilterable: true,
        }, {
            field: 'default',
            type: 'Boolean',
        }, {
            field: 'last4',
            type: 'String',
        }, {
            field: 'expireMonth',
            type: 'Number',
        }, {
            field: 'expireYear',
            type: 'Number',
        }],
    segments: [],
});

```

---

<div class="post-metadata">

**Author:** ![morganperre](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/morganperre/32/1296_2.png) [@morganperre](https://community.forestadmin.com/u/morganperre)\
**Post date:** [May 24, 2021, 3:39pm UTC](https://community.forestadmin.com/t/format-json-for-return-in-a-hasmany-smart-relationship/2544/6 "2021-05-24T15:39:00Z")

</div>

Hi again,

Really wierd I tried on my side and I’m able to make it work with your mock data.

 ![Screenshot 2021-05-24 at 17.29.22](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/8/85ab190cbc1e1096b2f3cf1ea73e271ec248708b.png)

1. How do you define the has many relationship in the **user** collection ? It should be something like this.

```javascript
{
  field: 'cards',
  type: ['String'],
  reference: 'cards.id'
}

```

1. Can you log the **result.data** object maybe it’s not a flat JSON (not the same you shared me).
2. Can you look at the call **/user/:userId/relationships/cards** on your Network tab ? Add copy paste the response ?

I hope we will find the find the solution To be honest I’m a little bit stuck at this point. I don’t see what’s you are missing. 🙁

Best regards,  
Morgan

---

<div class="post-metadata">

**Author:** ![Benjamin](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/benjamin/32/2127_2.png) [@Benjamin](https://community.forestadmin.com/u/Benjamin)\
**Post date:** [May 24, 2021, 4:07pm UTC](https://community.forestadmin.com/t/format-json-for-return-in-a-hasmany-smart-relationship/2544/7 "2021-05-24T16:07:12Z")

</div>

Okay that’s good !

I put user.id

```javascript
fields: [{
    field: 'cards',
    type: ['String'],
    reference: 'user.id'
  }],

```

With cards it works.

Thank you !

---

<div class="post-metadata">

**Author:** ![morganperre](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/morganperre/32/1296_2.png) [@morganperre](https://community.forestadmin.com/u/morganperre)\
**Post date:** [May 24, 2021, 4:27pm UTC](https://community.forestadmin.com/t/format-json-for-return-in-a-hasmany-smart-relationship/2544/8 "2021-05-24T16:27:06Z")

</div>

Really happy to hear that.

You’re welcome. Have a nice day.

Cheers,  
Morgan
