Relationships inside a nested "attribute"

Imagine a JSONApi resource that keeps track of all data changes by saving the old and new value of each property that has changed.

A simple record that should be compliant to JSONApi might look like this:

{
  "id": "1",
  "type": "auditlog",
  "attributes": {
    "diff": [
      {
        "property": "name",
        "old": "Old Name",
        "new": "New Name"
      }  
    ]
  },
  "relationships": {
    "performedOn": {
      "data": {
        "id": "2",
        "type": "customer"
      }
    },
    "performedBy": {
      "data": {
        "id": "3",
        "type": "user"
      }
    }
  }
}

I am not sure how to express a change where “old” and “new” is not a simple value but a relationship. The following is definitely not valid JSONApi (at least not in a way that enables clients to automatically fetch “old” and “new” data…)

{
  [...]
  "attributes": {
    "diff": [
      {
        "property": "status",
        "old": {
          "id": "4",
          "type": "customerStatus"
        },
        "new": {
          "id": "5",
          "type": "customerStatus"
        }
      }  
    ]
  }
  [...]
}

How would you do this?

Thanks!

I think you may consider to store that old information in meta data

This is just another example of why I am saying “Dear JSON:API, it’s over. It’s not me. It’s you.”

  1. Hierarchical data is poorly represented with JSON API
  2. Results of executing graph algorithms against a graph is poorly represented
  3. Too many things have to go in the “catch-all” meta-data bucket.

If your underlying API returns a deep hierarchical object you have to unravel the whole thing for Json API to represent it and then your client has to do the work of dealing with the unraveled flatted response.

Yea, I experienced with that. I had to wrote a parser in client-side to map all data, and wrote a query helper base on that map to deal with hierarchical data.
For big and complex data, it’s necessary to have a very good library for that work, or the application will cost a lot of resources to just process JSON-API data.