Hello everyone,
I’m currently facing the following issue:
I have an input parameter that is a JSON object containing many categories and attributes (nested structures and lists).
Now I need to trim all string values inside this JSON (remove leading and trailing spaces).
The straightforward approach is to deserialize the JSON and manually apply Trim() to every single attribute in every structure and list.
However, this is extremely time-consuming because the structure is very large and has many nested levels.
Question:
Is there any faster or more efficient way to trim all string values in such a JSON input in OutSystems? For example, a generic solution or a best practice that avoids writing trim logic for each field individually? Any suggestions or examples would be greatly appreciated.
Thank you!
@Bao Nguyen Khac : The solution is deserializing the JSON and trimming individual string values. This is achieved using an Extension that first parses the JSON text into a tree structure with Newtonsoft.Json . The code then recursively walks through every node in the tree. When a node represents a string value, it is trimmed; when a node represents an object or an array, its child nodes are iterated and processed in the same way.
I have attached the .NET code and the extension used for reference. Please note that the code was AI-generated and should be reviewed and adjusted as necessary before being used in a production environment.
Hello @Bao Nguyen Khac,
You can try this Javascript code. It checks for any value of type string inside the JSON, including values nested inside arrays or objects.I’ve attached a screenshot, and the script itself:
function trimStrings(value) {
if (typeof value === "string") {
return value.trim();
}
if (Array.isArray(value)) {
return value.map(trimStrings);
if (value !== null && typeof value === "object") {
Object.keys(value).forEach(function (key) {
value[key] = trimStrings(value[key]);
});
return value;
var obj = JSON.parse($parameters.Json);
var trimmedObj = trimStrings(obj);
$parameters.Json = JSON.stringify(trimmedObj);
$parameters.TrimmedJson = $parameters.Json;
@Sherif El-Habibi ,
I doubt this is something he needs at client side.
There are two possible cases: it could be a consumed POST method available in a UI module, in which case using JS is fine, since this method can be placed in a client action. Alternatively, it might be another server action that isn’t an API but is still used in a UI module. This might be a strange approach to get JSON format from a UI, but hey, it’s still a scenario. However, as you said, if all of this were implemented entirely in a service layer and not needed on the client side, then using JS wouldn’t help at all.
Thank everyone for your help!!!