JSON.ReplaceEvaluate
--------------------

Searches for all values that match the JSONPath expression and replaces them with the result of the evaluation.

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

MBS( "JSON.ReplaceEvaluate"; JSON; Path; Expression ) 

**MBS**( **"JSON.ReplaceEvaluate";** /\* Searches for all values that match the JSONPath expression and replaces them with the result of the evaluation. \*/  
**$JSON**; /\* A JSON text or reference.e.g. "{\\"people\\":\[{\\"first\\":\\"Christian\\",\\"last\\":\\"Schmitz\\",\\"city\\":\\"Nickenich\\"}\]}" \*/   
**$Path**; /\* The path to query.e.g. "$.books\[?(@.title == 'A Wild Sheep Chase')\].price" \*/   
**$Expression**) /\* The expression to run.  
You may call [JSON.CurrentMatch](JSONCurrentMatch.md) function to get current JSON as well as [JSON.CurrentMatchPath](JSONCurrentMatchPath.md) function to get the path. \*/ 

### Parameters

| Parameter | Description | Example |
|---|---|---|
| JSON | A JSON text or reference. | "{\\"people\\":\[{\\"first\\":\\"Christian\\",\\"last\\":\\"Schmitz\\",\\"city\\":\\"Nickenich\\"}\]}" |
| Path | The path to query. | "$.books\[?(@.title == 'A Wild Sheep Chase')\].price" |
| Expression | The expression to run.   You may call [JSON.CurrentMatch](JSONCurrentMatch.md) function to get current JSON as well as [JSON.CurrentMatchPath](JSONCurrentMatchPath.md) function to get the path. |  |

### Result

Returns JSON or error.

### Description

Searches for all values that match the JSONPath expression and replaces them with the result of the evaluation.  
Returns an error if JSONPath evaluation fails.  
  
See also [JSON.Query](JSONQuery.md) to just search without replace and [JSON.Replace](JSONReplace.md) to replace without expression.  
If the expression is not compiling, we return an error. Your expression should return the new JSON to replace the found item. But you can also return empty text to skip this.  
### Examples

Pass through current match:

 MBS ("JSON.ReplaceEvaluate"; JSON::JSON1 ; "$\[?(@.PrimaryKey)\]"; "Let(\[json = MBS (\\"[JSON.CurrentMatch](JSONCurrentMatch.md)\\")\]; json")  
Replace all found records with 123:

 MBS ("JSON.ReplaceEvaluate"; JSON::JSON1 ; "$\[?(@.PrimaryKey)\]"; "123")  
Collect all the match paths on the way:

 Let(\[  
$list = "";  
r = MBS ("JSON.ReplaceEvaluate"; JSON::JSON1 ; "$\[?(@.PrimaryKey)\]"; "Let(\[$list = $list &amp; MBS (\\"[JSON.CurrentMatchPath](JSONCurrentMatchPath.md)\\") &amp; \\¶\]; \\"\\")")  
\]; $list )  
Collect primary keys we find on the way:

 Let(\[  
$list = "";  
r = MBS ("JSON.ReplaceEvaluate"; JSON::JSON1 ; "$\[?(@.PrimaryKey)\]"; "Let(\[  
json = MBS (\\"[JSON.CurrentMatch](JSONCurrentMatch.md)\\");   
key = MBS ( \\"[JSON.GetPathItem](JSONGetPathItem.md)\\"; json; \\"PrimaryKey\\"; 1 );   
$list = $list &amp; key &amp; \\¶  
\]; \\"\\")")  
\]; $list )  
// Normally you do this with [JSON.Query](JSONQuery.md) function more efficiently!  
Now search objects with PrimaryKey and add entries for Assignments:

 MBS ("JSON.ReplaceEvaluate"; JSON::JSON1 ; "$\[?(@.PrimaryKey)\]"; "Let(\[  
/\*the current match \*/   
$json = MBS (\\"[JSON.CurrentMatch](JSONCurrentMatch.md)\\");   
/\* now lookup primary key inside it \*/   
$key = MBS ( \\"[JSON.GetPathItem](JSONGetPathItem.md)\\"; $json ; \\"PrimaryKey\\"; 1 );   
/\* look for related records \*/   
$sql = MBS ( \\"[FM.SQL.Execute](FMSQLExecute.md)\\"; Get(FileName); \\"SELECT Name, Note, PrimaryKey, EmployeeForeignKey, \\\\\\"Date Returned\\\\\\" FROM Assignments WHERE AssetForeignKey = ?\\"; $key );  
/\* now get a JSON array for the found set \*/   
foundJSON = MBS ( \\"[FM.SQL.JSONRecords](FMSQLJSONRecords.md)\\"; $sql ; \\"Name\\¶Note\\¶AssignmentsPrimaryKey\\¶EmployeeForeignKey\\¶Date Returned\\" );  
/\* and cleanup memory \*/   
$r = MBS (\\"[FM.SQL.Release](FMSQLRelease.md)\\"; $sql );  
/\* build new JSON with the related records added \*/   
$newjson = MBS (\\"[JSON.AddItemToObject](JSONAddItemToObject.md)\\"; $json ; \\"Assignments\\"; foundJSON )  
\]; $newjson )")  
Now search objects with PrimaryKey and add entries for Employees on the new Assignments:

 MBS ("JSON.ReplaceEvaluate"; JSON::JSON3 ; "$\[\*\].Assignments\[?@.AssignmentsPrimaryKey\]"; "Let(\[  
/\*the current match \*/   
$json = MBS (\\"[JSON.CurrentMatch](JSONCurrentMatch.md)\\");   
/\* now lookup primary key inside it \*/   
$key = MBS ( \\"[JSON.GetPathItem](JSONGetPathItem.md)\\"; $json ; \\"EmployeeForeignKey\\"; 1 );   
/\* look for related records \*/   
$sql = MBS ( \\"[FM.SQL.Execute](FMSQLExecute.md)\\"; Get(FileName); \\"SELECT Name, PrimaryKey, \\\\\\"First Name\\\\\\" FROM Employees WHERE PrimaryKey = ?\\"; $key );  
/\* now get a JSON array for the found set \*/   
foundJSON = MBS ( \\"[FM.SQL.JSONRecords](FMSQLJSONRecords.md)\\"; $sql ; \\"Name\\¶PrimaryKey\\¶First Name\\" );  
/\* and cleanup memory \*/   
$r = MBS (\\"[FM.SQL.Release](FMSQLRelease.md)\\"; $sql );  
/\* build new JSON with the related records added \*/   
$newjson = MBS (\\"[JSON.AddItemToObject](JSONAddItemToObject.md)\\"; $json ; \\"Employees\\"; foundJSON )  
\]; $newjson )")  
Add one to each entry in an array:

 MBS ( "JSON.ReplaceEvaluate"; "\[1,2\]"; "$\[\*\]"; " MBS (\\"[JSON.CurrentMatch](JSONCurrentMatch.md)\\") + 1" )  
<small>  
Example result:   
\[ 2 , 3 \] </small>### See also

- [FM.SQL.Execute](FMSQLExecute.md)
- [FM.SQL.JSONRecords](FMSQLJSONRecords.md)
- [FM.SQL.Release](FMSQLRelease.md)
- [JSON.AddItemToObject](JSONAddItemToObject.md)
- [JSON.CurrentMatch](JSONCurrentMatch.md)
- [JSON.CurrentMatchPath](JSONCurrentMatchPath.md)
- [JSON.InsertOrUpdateRecord](JSONInsertOrUpdateRecord.md)
- [JSON.InsertRecord](JSONInsertRecord.md)
- [JSON.Query](JSONQuery.md)
- [JSON.Replace](JSONReplace.md)

### Release notes

- **Version 14.2**
    - Added [JSON.ReplaceEvaluate](https://www.mbsplugins.eu/JSONReplaceEvaluate.shtml) function and related [JSON.CurrentMatch](https://www.mbsplugins.eu/JSONCurrentMatch.shtml) and [JSON.CurrentMatchPath](https://www.mbsplugins.eu/JSONCurrentMatchPath.shtml) functions.

### Blog Entries

- [News in MBS FileMaker Plugin 14.2](https://www.mbsplugins.de/archive/2024-05-21/News_in_MBS_FileMaker_Plugin_1/monkeybreadsoftware_blog_filemaker)
- [Neues MBS Plugin 14.2 für Claris FileMaker](https://www.mbsplugins.de/archive/2024-05-14/Neues_MBS_Plugin_142_für_Clar/monkeybreadsoftware_blog_filemaker)
- [MBS Plugin 14.2 for Claris FileMaker](https://www.mbsplugins.de/archive/2024-05-14/MBS_Plugin_142_for_Claris_File/monkeybreadsoftware_blog_filemaker)
- [JSON Replace with evaluate](https://www.mbsplugins.de/archive/2024-05-06/JSON_Replace_with_evaluate/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 14.2pr4](https://www.mbsplugins.de/archive/2024-05-02/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)

This function checks for a license.

Created 30th April 2024 , last changed 15th May 2024

  
[JSON.Replace](JSONReplace.md) - [JSON.ReplaceItemInArray](JSONReplaceItemInArray.md)

[HTML Version](JSONReplaceEvaluate.shtml)