What is the best way to handle nested relationships?

Hi all, I have a ResourceA with nested objects:

{
  "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:

{
  "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 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?

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.