Predict the output
Apply a merge patch. What's the result?
Doc = dict[str, object]
def merge(target: Doc, patch: Doc) -> Doc:
out = dict(target)
for k, v in patch.items():
if v is None:
out.pop(k, None)
else:
out[k] = v
return out
doc: Doc = {"name": "Ana", "nick": "A", "tags": [1, 2]}
print(merge(doc, {"nick": None, "tags": [3]}))Hint 1 of 1
What does null mean in a merge patch?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A {'name': 'Ana', 'tags': [3]}
Correct. null removes nick; the array is replaced, not merged.
JSON Merge Patch (RFC 7396) has two rules here: a null value deletes the key, and any other value, including an array, replaces what was there. Keys missing from the patch stay as they were.
Why not the others?
- B
{'name': 'Ana', 'nick': None, 'tags': [3]}. In merge patch, null means delete. - C
{'name': 'Ana', 'tags': [1, 2, 3]}. Arrays are replaced wholesale. - D
{'nick': None, 'tags': [3]}. Fields not in the patch stay.