Idefixx
Idefixx

Reputation: 472

Handling nulls in JSON Merge Patch

I am using application/merge-patch+json as my content type to edit a resource via HTTP PATCH method. I got my data stored in an RDBMS.

The merge patch spec clearly states that when a key is set to a null value, it should be deleted from the resource.

This is impossible for me to do however, as I cannot simply delete columns in my DB for certain rows - the values I 'delete' are simply set to null, which goes against the spec.

I thought of two options:

  1. Go against the spec and simply set the columns to null, keeping them in the resource
  2. Use JSON patch instead, which is quite an overhead compared to merge patch

Is there some other way?

Upvotes: 1

Views: 2104

Answers (2)

Robert W.
Robert W.

Reputation: 340

The RFC doesn't define how you store your data, it's only about JSON. API/Domain Models should be separated from Entity Layer / DB. How you map between these two, is up to you.

Upvotes: 1

Alex Marculescu
Alex Marculescu

Reputation: 5770

A DELETE on an API resource doesn't have to be actually deleting the resource in the DB - it could be a soft delete for that matter (which is what you're doing, in fact).

As long as it's properly represented as deleted by the API (e.g. you can omit displaying null values - I think JSON does that for you by default), it can be argued that you're respecting the spec.

Upvotes: 2

Related Questions