22
Views
5
Comments
Solved
How to trim data json

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!

2019-01-07 16-04-16
Siya
 
MVP
Solution

@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.


NssTrimJson.txt
TrimJson_v2.xif
2025-12-22 13-50-43
Sherif El-Habibi
Champion

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;

  }

  return value;

}

var obj = JSON.parse($parameters.Json);

var trimmedObj = trimStrings(obj);

$parameters.Json = JSON.stringify(trimmedObj);

$parameters.TrimmedJson = $parameters.Json;


2021-09-06 15-09-53
Dorine Boudry
 
MVP

@Sherif El-Habibi ,

I doubt this is something he needs at client side.

2025-12-22 13-50-43
Sherif El-Habibi
Champion

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.

2019-01-07 16-04-16
Siya
 
MVP
Solution

@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.


NssTrimJson.txt
TrimJson_v2.xif
UserImage.jpg
Bao Nguyen Khac

Thank everyone for your help!!!

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.