A Dart implementation of RFC 7396 — JSON Merge Patch.
mergePatch(target, patch)— Apply a merge patch to a JSON value.generate(source, target)— Create a merge patch that turnssourceintotarget.- No external dependencies.
- Dart 3.0+.
- Passes all 16 test cases from RFC 7396 Appendix A.
dart pub add json_merge_patchimport 'package:json_merge_patch/json_merge_patch.dart';
void main() {
// Apply a merge patch
final target = {'a': 'b', 'c': {'d': 'e', 'f': 'g'}};
final patch = {'a': 'z', 'c': {'f': null}};
final result = mergePatch(target, patch);
print(result); // {a: z, c: {d: e}}
// Generate a merge patch
final source = {'a': 'b', 'c': 'd'};
final goal = {'a': 'z', 'e': 'f'};
final generated = generate(source, goal);
print(generated); // {a: z, c: null, e: f}
}RFC 7396 defines a recursive algorithm for partial JSON updates:
- If the patch is an object, merge it recursively with the target.
- If a patch value is
null, remove that key from the target. - If the patch is not an object, replace the target entirely.
Arrays are always replaced wholesale (not merged element-by-element), and null cannot be set as a value — it always means "delete".
These come from the RFC 7396 spec itself:
nullmeans delete — you can't set a field's value tonull.- No array merging — arrays are replaced entirely.
- No conditional updates — there's no
testoperation like RFC 6902.
If you need those, see rfc_6902.
Implements the MergePatch algorithm from RFC 7396 Section 2. All 16 Appendix A test cases are included and pass.
MIT. See LICENSE.