# What is the best way to handle nested relationships?

**URL:** <https://discuss.jsonapi.org/t/what-is-the-best-way-to-handle-nested-relationships/2582>\
**Category:** Uncategorized\
**Created:** [December 16, 2022, 3:16pm UTC](https://discuss.jsonapi.org/t/what-is-the-best-way-to-handle-nested-relationships/2582 "2022-12-16T15:16:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sergey](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@sergey](https://discuss.jsonapi.org/u/sergey)\
**Post date:** [December 16, 2022, 3:16pm UTC](https://discuss.jsonapi.org/t/what-is-the-best-way-to-handle-nested-relationships/2582/1 "2022-12-16T15:16:18Z")

</div>

Hi all, I have a **ResourceA** with nested objects:

```auto
{
  "data": [
    {
      "type": "resource_a",
      "id": "some-unique-id-here",
      "attributes": {
        "key1": "value1",
        "key2": "value2",
        "fee": {
          "price": 1245.56,
          "value": 123.99
        },
        "changes": [
          {
            "value": 0,
            "price": 0
          },
          ...
        ]
      }
    }
  ]
}

```

Everything is ok, because specification says:

> Attributes may contain any valid JSON value, including complex data structures involving JSON objects and arrays.

But I also have to add related **ResourceB** to both fee and every change in the list:

```auto
{
  "data": [
    {
      "type": "resource_a",
      "id": "some-unique-id-here",
      "attributes": {
        "key1": "value1",
        "key2": "value2",
        "fee": {
          "resource_b": ????,
          "price": 2542.23,
          "value": 39.965672931101786
        },
        "changes": [
          {
            "resource_b": ????,
            "value": 0,
            "price": 0
          }
        ]
      }
    }
  ]
}

```

But it does not seem valid, because the specification says:

> Keys that reference related resources (e.g. `author_id`) **SHOULD NOT** appear as attributes. Instead, [relationships](https://jsonapi.org/format/1.2/#document-resource-object-relationships) **SHOULD** be used.

Thus I can’t add nor resourceb\_id or resource object itself. I don’t have any separate resources for fees and changes, how can I handle this use case?

---

<div class="post-metadata">

**Author:** ![jelhan](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/jelhan/32/822_2.png) [@jelhan](https://discuss.jsonapi.org/u/jelhan)\
**Post date:** [December 17, 2022, 8:08pm UTC](https://discuss.jsonapi.org/t/what-is-the-best-way-to-handle-nested-relationships/2582/2 "2022-12-17T20:08:08Z")

</div>

Using complex data structures for attribute values could be a footgun. Another resource (and the relationship to it) may be represented as an attribute having a complex data structure. Doing so will cause issues as soon as you need to add relationship to them as well. Or if you need to support partial updates of them. I would highly recommend to use them with care.

You seem to face such a case. I would recommend to model `fee` and `changes` as resource types, which are having a relationship both to `resource_a` and `resource_b`.
