Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

JSON.JSONPatch.FromDiff

Create a JSON Patch from a diff of two json documents.

Component Version macOS Windows Linux Server iOS SDK
JSON 14.0 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "JSON.JSONPatch.FromDiff"; json1; json2 { ; KeyToCopy } )   More

Parameters

Parameter Description Example Flags
json1 A JSON text or reference.
json2 A JSON text or reference.
KeyToCopy Available in MBS FileMaker Plugin 14.1 or newer.
When we create our change record, we can optionally carry over an extra field for the changes.
"pkey" Optional

Result

Returns JSON or error.

Description

Create a JSON Patch from a diff of two json documents.
Returns a JSON Patch.

The jsonpatch functions implement the IETF standard JavaScript Object Notation (JSON) Patch.

The JSON Patch IETF standard requires that the JSON Patch method is atomic, so that if any JSON Patch operation results in an error, the target document is unchanged. The patch function implements this requirement by generating the inverse commands and building an undo stack, which is executed if any part of the patch fails.

Use JSON.JSONPatch.ApplyPatch to apply the patch to JSON data later.

Examples

Create a patch:

MBS( "JSON.JSONPatch.FromDiff";
"{\"people\":[{\"first\":\"Christian\",\"last\":\"Miller\",\"city\":\"Nickenich\"}]}";
"{\"people\":[{\"first\":\"Peter\",\"city\":\"Nickenich\", \"zip\": 56645 }]}" )

Example result:
[ { "op": "replace", "path": "/people/0/first", "value": "Peter" }, { "op": "remove", "path": "/people/0/last" }, { "op": "add", "path": "/people/0/zip", "value": 56645 } ]

Try with additional key to copy:

Let ( [
json1 = "{\"people\":[{\"first\":\"Christian\",\"last\":\"Miller\",\"city\":\"Nickenich\",\"pkey\":\"EAE6757654564DEAC\"}]}";
json2 = "{\"people\":[{\"first\":\"Peter\",\"city\":\"Nickenich\", \"zip\": 56645, \"pkey\":\"EAE6757654564DEAC\" }]}";
patch = MBS( "JSON.JSONPatch.FromDiff"; json1; json2; "pkey" )]; patch )

Example result:
[ { "op": "replace", "path": "/people/0/first", "value": "Peter", "pkey": "EAE6757654564DEAC" }, { "op": "remove", "path": "/people/0/last", "pkey": "EAE6757654564DEAC" }, { "op": "add", "path": "/people/0/zip", "value": 56645, "pkey": "EAE6757654564DEAC" } ]

See also

Release notes

Blog Entries

This function checks for a license.

Created 23th November 2023, last changed 31st January 2024


JSON.JSONPatch.ApplyPatch - JSON.List

💬 Ask a question or report a problem