JSON.JSONPatch.FromDiff
-----------------------

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

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [JSON](component_JSON.md) | [14.0](newinversion140.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "JSON.JSONPatch.FromDiff"; json1; json2 { ; KeyToCopy } ) 

**MBS**( **"JSON.JSONPatch.FromDiff";** /\* Create a JSON Patch from a diff of two json documents. \*/  
**$json1**; /\* A JSON text or reference. \*/   
**$json2**; /\* A JSON text or reference. \*/   
**$KeyToCopy**) /\* Optional; When we create our change record, we can optionally carry over an extra field for the changes.e.g. "pkey" \*/ 

### Parameters

| Parameter | Description | Example | Flags |
|---|---|---|---|
| json1 | A JSON text or reference. |  |  |
| json2 | A JSON text or reference. |  |  |
| KeyToCopy | When we create our change record, we can optionally carry over an extra field for the changes. | "pkey" | Optional      Added in version **14.1**. |

### 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](JSONJSONPatchApplyPatch.md) 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 }\]}" )  
<small>  
Example result:   
\[ { "op" : "replace" , "path" : "/people/0/first" , "value" : "Peter" }, { "op" : "remove" , "path" : "/people/0/last" }, { "op" : "add" , "path" : "/people/0/zip" , "value" : 56645 } \] </small>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 )  
<small>  
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" } \] </small>### See also

- [JSON.JSONPatch.ApplyPatch](JSONJSONPatchApplyPatch.md)
- [JSON.MergePatch.FromDiff](JSONMergePatchFromDiff.md)

### Release notes

- **Version 15.3**
    - Fixed [JSON.JSONPatch.FromDiff](https://www.mbsplugins.eu/JSONJSONPatchFromDiff.shtml) to do the copy key feature again, broken with jsoncons update in version 15.1.
- **Version 14.1**
    - Added KeyToCopy parameter to [JSON.JSONPatch.FromDiff](https://www.mbsplugins.eu/JSONJSONPatchFromDiff.shtml) to copy primary keys.
- **Version 14.0**
    - Added [JSON.JSONPatch.ApplyPatch](https://www.mbsplugins.eu/JSONJSONPatchApplyPatch.shtml), [JSON.JSONPatch.FromDiff](https://www.mbsplugins.eu/JSONJSONPatchFromDiff.shtml), [JSON.MergePatch.ApplyPatch](https://www.mbsplugins.eu/JSONMergePatchApplyPatch.shtml) and [JSON.MergePatch.FromDiff](https://www.mbsplugins.eu/JSONMergePatchFromDiff.shtml) functions.

### Blog Entries

- [MBS FileMaker Plugin, version 15.3pr2](https://www.mbsplugins.de/archive/2025-06-09/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [Neues MBS Plugin 14.1 für Claris FileMaker](https://www.mbsplugins.de/archive/2024-03-12/Neues_MBS_Plugin_141_für_Clar/monkeybreadsoftware_blog_filemaker)
- [MBS Plugin 14.1 for Claris FileMaker](https://www.mbsplugins.de/archive/2024-03-12/MBS_Plugin_141_for_Claris_File/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 14.1pr1](https://www.mbsplugins.de/archive/2024-02-01/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [New in MBS FileMaker Plugin 14.0](https://www.mbsplugins.de/archive/2024-01-23/New_in_MBS_FileMaker_Plugin_14/monkeybreadsoftware_blog_filemaker)
- [MBS @ FMTraining.TV - FileMaker Monkeybread Plug-in Q&amp;A](https://www.mbsplugins.de/archive/2023-12-09/MBS_@_FMTrainingTV_-_FileMaker/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 13.6pr1](https://www.mbsplugins.de/archive/2023-12-01/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)

This function checks for a license.

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

  
[JSON.JSONPatch.ApplyPatch](JSONJSONPatchApplyPatch.md) - [JSON.List](JSONList.md)

[HTML Version](JSONJSONPatchFromDiff.shtml)