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

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 13.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "JSON.Replace"; json; path; ReplaceJSON )   More

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 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)]"; "\"\"" )

Example result:
{ "author": "test", "category": "", "price": "", "title": "test" }

Replace null vaules in array with 0:

MBS( "JSON.Replace"; "[1,2,null,3,4]"; "$..[?(@ == null)]"; "0" )

Example result:
[ 1, 2, 0, 3, 4 ]

Replace matches using contains:

MBS("JSON.Replace"; "[\"test\", \"abc\", \"ast\"]"; "$[?(contains(@,'s'))]"; "\"match\"")

Example result:
[ "match", "abc", "match" ]

See also

Release notes

Example Databases

Blog Entries

This function checks for a license.

Created 17th September 2023, last changed 22nd July 2024


JSON.ReleaseAll - JSON.ReplaceEvaluate