JSON.Replace
------------

Searches for all values that match the JSONPath expression and replaces them with the specified value.

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

MBS( "JSON.Replace"; JSON; Path; ReplaceJSON ) 

**MBS**( **"JSON.Replace";** /\* Searches for all values that match the JSONPath expression and replaces them with the specified value. \*/  
**$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" \*/   
**$ReplaceJSON**) /\* The new JSON value.e.g. 123 \*/ 

### 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" |
| ReplaceJSON | The new JSON value. | 123 |

### Result

Returns JSON or error.

### Description

Searches for all values that match the JSONPath expression and replaces them with the specified value.  
Returns an error if JSONPath evaluation fails.  
  
See also [JSON.Query](JSONQuery.md) to just search without replace.  
### Examples

Find an entry in the books array and change the value:

 Let(\[  
json = "{  
 \\"books\\": \[  
 {  
 \\"author\\": \\"Haruki Murakami\\",  
 \\"category\\": \\"fiction\\",  
 \\"price\\": 20.0 ,  
 \\"title\\": \\"A Wild Sheep Chase\\"  
 },  
 {  
 \\"author\\": \\"Sergei Lukyanenko\\",  
 \\"category\\": \\"fiction\\",  
 \\"price\\": 23.58 ,  
 \\"title\\": \\"The Night Watch\\"  
 },  
 {  
 \\"author\\": \\"Graham Greene\\",  
 \\"category\\": \\"fiction\\",  
 \\"price\\": 21.99 ,  
 \\"title\\": \\"The Comedians\\"  
 },  
 {  
 \\"author\\": \\"Phillips, David Atlee\\",  
 \\"category\\": \\"memoir\\",  
 \\"title\\": \\"The Night Watch\\"  
 }  
 \]  
}";  
output = MBS ( "JSON.Replace"; json; "$.books\[?(@.title == 'A Wild Sheep Chase')\].price"; 123 )  
\]; output)  
Replace null values in object with empty quotes:

 MBS ( "JSON.Replace"; "{  
\\"author\\": \\"test\\",  
\\"category\\": null,  
\\"price\\": null,  
\\"title\\": \\"test\\"  
}"; "$..\[?(@ == null)\]"; "\\"\\"" )  
<small>  
Example result:   
{ "author" : "test" , "category" : "" , "price" : "" , "title" : "test" } </small>Replace null vaules in array with 0:

 MBS ( "JSON.Replace"; "\[1,2,null,3,4\]"; "$..\[?(@ == null)\]"; "0" )  
<small>  
Example result:   
\[ 1 , 2 , 0 , 3 , 4 \] </small>Replace matches using contains:

 MBS ("JSON.Replace"; "\[\\"test\\", \\"abc\\", \\"ast\\"\]"; "$\[?(contains(@,'s'))\]"; "\\"match\\"")  
<small>  
Example result:   
\[ "match" , "abc" , "match" \] </small>Change colors in styled text:

 # styled text to JSON   
Set Variable \[ $json ; Value: MBS ("[Text.Styles](TextStyles.md)"; Test::Input ) \]  
\# # change all red to 123   
Set Variable \[ $json ; Value: MBS ("JSON.Replace"; $json ; "$..red"; 123) \]  
\# # change all green to 0   
Set Variable \[ $json ; Value: MBS ("JSON.Replace"; $json ; "$..green"; 0) \]  
\# # convert back to styled text   
Set Field \[ Test::Output ; MBS ("[Text.FromStyles](TextFromStyles.md)"; $json ) \]  
### See also

- [JSON.CurrentMatch](JSONCurrentMatch.md)
- [JSON.CurrentMatchPath](JSONCurrentMatchPath.md)
- [JSON.InsertOrUpdateRecord](JSONInsertOrUpdateRecord.md)
- [JSON.InsertRecord](JSONInsertRecord.md)
- [JSON.Query](JSONQuery.md)
- [JSON.Release](JSONRelease.md)
- [JSON.ReplaceEvaluate](JSONReplaceEvaluate.md)
- [JSON.ReplaceItemInArray](JSONReplaceItemInArray.md)
- [JSON.ReplaceItemInObject](JSONReplaceItemInObject.md)
- [Text.Styles](TextStyles.md)

### Release notes

- **Version 14.4**
    - Improved [JSON](https://www.mbsplugins.eu/component_JSON.shtml) functions to return error if you pass non JSON parameters to [JSON.AddItemToArray](https://www.mbsplugins.eu/JSONAddItemToArray.shtml), [JSON.AddItemToObject](https://www.mbsplugins.eu/JSONAddItemToObject.shtml), [JSON.Replace](https://www.mbsplugins.eu/JSONReplace.shtml) and related functions.
- **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.
- **Version 14.1**
    - Changed [JSON.Replace](https://www.mbsplugins.eu/JSONReplace.shtml) to accept text or number as replacement and convert it to JSON internally automatically instead of returning a parsing error.
    - Fixed an edge case with [JSON.Replace](https://www.mbsplugins.eu/JSONReplace.shtml) where it didn't catch all spots.
- **Version 13.5**
    - Added [JSON.Replace](https://www.mbsplugins.eu/JSONReplace.shtml) to replace within a JSON.

### Example Databases

- [JSON/JSON Query](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/JSON/JSON%20Query.shtml#2ScriptAnchor_)

### Blog Entries

- [MBS FileMaker Plugin, version 14.4pr6](https://www.mbsplugins.de/archive/2024-09-11/MBS_FileMaker_Plugin_version_1/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 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.1pr5](https://www.mbsplugins.de/archive/2024-03-01/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [New in MBS FileMaker Plugin 13.5](https://www.mbsplugins.de/archive/2023-11-28/New_in_MBS_FileMaker_Plugin_13/monkeybreadsoftware_blog_filemaker)
- [MBS Plugin 13.5 for Claris FileMaker](https://www.mbsplugins.de/archive/2023-11-07/MBS_Plugin_135_for_Claris_File/monkeybreadsoftware_blog_filemaker)
- [Using JSONPath in FileMaker](https://www.mbsplugins.de/archive/2023-10-18/Using_JSONPath_in_FileMaker/monkeybreadsoftware_blog_filemaker)
- [Checking out JSON.Search function in MBS FileMaker Plugin](https://www.mbsplugins.de/archive/2023-10-17/Checking_out_JSONSearch_functi/monkeybreadsoftware_blog_filemaker)
- [JSON Query in FileMaker](https://www.mbsplugins.de/archive/2023-09-29/JSON_Query_in_FileMaker/monkeybreadsoftware_blog_filemaker)

### FileMaker Magazin

- [Ausgabe 5/2020, Seite 17](https://filemaker-magazin.de/neuigkeit/4110-Appetithappen-FMM_202005)

This function checks for a license.

Created 17th September 2023 , last changed 23th November 2025

  
[JSON.ReleaseAll](JSONReleaseAll.md) - [JSON.ReplaceEvaluate](JSONReplaceEvaluate.md)

[HTML Version](JSONReplace.shtml)