# How to represent deeply nested objects/resources?

**URL:** <https://discuss.jsonapi.org/t/how-to-represent-deeply-nested-objects-resources/1782>\
**Category:** Uncategorized\
**Created:** [December 9, 2019, 10:03pm UTC](https://discuss.jsonapi.org/t/how-to-represent-deeply-nested-objects-resources/1782 "2019-12-09T22:03:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![v1k45](https://avatars.discourse-cdn.com/v4/letter/v/85f322/32.png) [@v1k45](https://discuss.jsonapi.org/u/v1k45)\
**Post date:** [December 9, 2019, 10:03pm UTC](https://discuss.jsonapi.org/t/how-to-represent-deeply-nested-objects-resources/1782/1 "2019-12-09T22:03:43Z")

</div>

Hi, I’d like to know your thoughts on how to represent deeply nested objects.

From what I understand, all related objects should be returned from the `relationships` field. What is better when we have reasonably deep related object, `relationships` or `attributes`?

For example:

Representing it as a complex object:

```auto
{
  "data": {
    "type": "type-a",
    "id": 1,
    "attributes": {
      "type_b_list": [{
       "id": 2,
       "type_c_list": [{
         "id": 3,
         "type_d_list": [],
         "type_e_list": []
       }]
      }]
    }
  }
}

```

Representing proper relationships to follow the spec/sematics:

```auto
{
  "data": {
    "type": "type-a",
    "id": 1,
    "relationships": {
      "type_b_list": {
        "id": 2,
        "type": "type-b"
      }
    }
  },
  "included": [
    {
        "id": 2,
        "type": "type-b",
        "relationships": {
          "type_c_list": [
            {
              "id": 3,
              "type": "type-c"
            }
          ]
        }
    }
  ] 
}

```

The the second example, how does `include` work with deeply nested objects? What If I’d like to be able to return upto `type-e` depth (or even more)?

I don’t think having multiple round trips to get each related resource in separate request is feasible.

Other alternative would be to follow the JSON:API resource object structure (type/id/attributes etc.) in the example 1 when representing nested objects.

What do you think?

---

<div class="post-metadata">

**Author:** ![maark](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/maark/32/184_2.png) [@maark](https://discuss.jsonapi.org/u/maark)\
**Post date:** [December 14, 2019, 3:47pm UTC](https://discuss.jsonapi.org/t/how-to-represent-deeply-nested-objects-resources/1782/2 "2019-12-14T15:47:33Z")

</div>

Your 2nd is example is the correct way, not the first one.

If you want to go deeper, you can also include your `type-c` resource since it is mentioned in your `type-b` resource under the `included` top-level member. Then, keep going until you reach `type-e`.
