Replace special characters in JSON response

We have an issue where an API push is failing because we are receiving JSON data and then pushing this data straight back out to the API. The API can output special characters but won't accept them. To get around this, we need to find a way to replace certain special characters from value fields with encoded characters. Example:

Received JSON:

{test1: "this is a test with some valid characters",

test2: "this is a test with some invalid characters , ! £"}

Pushed JSON:

{test1: "this is a test with some valid characters",

test2: "this is a test with some invalid characters .co .ex .po"}

So in this instance the aim would be to replace those special characters at the end of test2.

Is there a way to do this, potentially with REGEX or a replace string? It would need to check all the value fields and replace any of the special characters in there with some encoding we've defined.

Apply clean() function to the incoming text either before you save or before you display . Regex() function resolves your issue atmost. As you want to replace the special characters with some other encoding, try using substitute() function.

Apply clean() function to the incoming text either before you save or before you display . Regex() function resolves your issue atmost. As you want to replace the special characters with some other encoding, try using substitute() function.