# PATCH Complex Data Structures

**URL:** https://discuss.jsonapi.org/t/patch-complex-data-structures/1148
**Category:** Uncategorized
**Created:** [November 2, 2017, 11:10am UTC](https://discuss.jsonapi.org/t/patch-complex-data-structures/1148 "2017-11-02T11:10:15Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Mahoney](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/mahoney/32/500_2.png) [@Mahoney](https://discuss.jsonapi.org/u/Mahoney)
#### Post date: [November 2, 2017, 11:10am UTC](https://discuss.jsonapi.org/t/patch-complex-data-structures/1148/1 "2017-11-02T11:10:15Z")

</div>

Hi all - I’ve got a complex resource which multiple nested one-to-many relationships, which is allowed by the spec: “Complex data structures involving JSON objects and arrays are allowed as attribute values”.

Data looks roughly like this:

```auto
{
  "type": "contracts",
  "id": "9b0ac85e-a74f-4d4a-8248-33e0ae3a137",
  "attributes": {
    "name": "My Contract Name",
    "startYearAndMonth": "2017-04",
    "services": {
      "Service Name 1": {
        "name": "Service Name 1",
        "incomeStreams": {
          "Training": {
            "name": "Training",
            "dataPoints": {
              "2017-04": { "value": 143987 },
              "2017-07": { "value": 134936 }
            }
          },
          "Sales": {
            "name": "Sales",
            "dataPoints": {
              "2017-04": { "value": 3454565 },
              "2017-07": { "value": 5464566 }
            }
          }
        }
      },
      "Service Name 2": {
        "name": "Service Name 2",
        "incomeStreams": {
          "Training": {
            "name": "Training",
            "dataPoints": {
              "2017-04": { "value": 143987 },
              "2017-07": { "value": 134936 }
            }
          },
          "Sales": {
            "name": "Sales",
            "dataPoints": {
              "2017-04": { "value": 3454565 },
              "2017-07": { "value": 5464566 }
            }
          }
        }
      }
    }
  }
}

```

I’m struggling with adding or changing a single data point in a `PATCH` request. Over-writing the entire `services` attribute feels unnecessarily brute force - harder to implement (need to over-write all that data) and more prone to conflict with concurrent editing. Can I use [JSON Patch](https://tools.ietf.org/html/rfc6902) for this purpose and still be JSON API compliant? It needs a different media type - `application/json-patch+json`.

The alternative I’ve come up with is to model `services`, `incomeStreams` and `dataPoints` as separate resources - then I can `POST` to the `dataPoints` collection or `PATCH` an existing `dataPoint` in isolation. But here I’m feeling the pain of creating a Contract in the first place - at the moment it’s a single POST to `/contracts`, but it would then need to be a barrage of `POST` requests to create each child item. As they are pure child, one-to-many relationships (no service is owned by another contract, no income stream by another service, no data point by another income stream) this seems overkill.

Grateful for any advice - I failed to find any advice on this score when searching.

---

<div class="post-metadata">

### Author: ![michaelhibay](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/michaelhibay/32/405_2.png) [@michaelhibay](https://discuss.jsonapi.org/u/michaelhibay)
#### Post date: [November 22, 2017, 9:35pm UTC](https://discuss.jsonapi.org/t/patch-complex-data-structures/1148/2 "2017-11-22T21:35:37Z")

</div>

Rob, JSON Patch is not your answer, {json:api} compound documents are!

You already have the right notion in your head via the alternative you offered. The problem you mentioned is actually not a problem at all, you can create compound resources and dependent resources using a [compound document](http://jsonapi.org/format/#document-compound-documents) as long as you supply the UUID for the newly created resource and use it as reference for your POST. The domain knowledge of the exclusive relationship between a service and a contract is clouding the picture for you as the design itself shouldn’t project that as a consideration as it could change later.

Let us know how it goes!

---

<div class="post-metadata">

### Author: ![Mahoney](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/mahoney/32/500_2.png) [@Mahoney](https://discuss.jsonapi.org/u/Mahoney)
#### Post date: [November 23, 2017, 2:43pm UTC](https://discuss.jsonapi.org/t/patch-complex-data-structures/1148/3 "2017-11-23T14:43:38Z")

</div>

Thanks very much - it wasn’t clear to me that a compound document could be a request entity as well as a response one. I will try that.
