57
Views
1
Comments
Handling Dynamic Data Types in JSON Response Fields from External APIs

Hi everyone,

I'm working on an integration service module in OutSystems, consuming a REST API provided by a third-party vendor. I'm facing a common challenge: the JSON response structure for one specific API endpoint can have a particular JSON key (in the nested JSON structure) with variable data types for its value (e.g., text, numbers, decimals, booleans, objects, or lists of any of these). 


The Problem:

In the JSON response from this external API endpoint, a specific field ("Value" is my key in question example key, but this could apply to any field name) can contain different JSON data types depending on the context or associated metadata. For instance, my "Value" field might come back as:

  • Number: "Value": 1544566
  • Decimal:  "Value": 12.0
  • Boolean: "Value": true
  • String: "Value": "Some text"
  • JSON Object: "Value": {"id": 1035871, "status": "active"}
  • JSON Array (of strings or objects): "Value": ["item1", "item2"] or "Value": [{"id":1}, {"name":"test"}]

OutSystems' JSON Deserialization action, when mapping to a defined Structure, expects a consistent, single data type for each attribute. 


My attempts:

After some research, I found that the OnAfterResponse Handler in the REST API method seems like the ideal place to implement this transformation. My initial thought was to use Regex_Replace from the Text extension to capture and modify these varying values. However, due to the complexity of JSON's nested structures and diverse data types, I've struggled to create a robust regex that consistently captures all possibilities.

I've run into issues with: 

  • Escaping in OutSystems String Literals: Handling double quotes (") and backslashes (\) within the regex pattern string in OutSystems expressions proved very difficult.
  • Regex Engine Limitations: Errors like "Unrecognized Grouping Construct" indicate that the underlying .NET regex engine in OutSystems does not fully support the advanced, recursive patterns needed for reliably parsing arbitrary JSON nesting.
  • Reliability: Even complex regex patterns struggled to consistently capture the entire value when it contained nested JSON objects or arrays, often stopping prematurely.

My ask:

Could someone please point me to the right direction as to how to handle this? I would appreciate some more detailed guidance since I am a relatively new OutSystems developer. Much appreciated!


Example:

Here's an example of the kind of JSON structure I'm dealing with, showing the Value field's variability: 

{

    "TotalCount": 1,

    "Objects": [

        {

            "ParentObject": {

                "ArtifactID": 10349035

            },

            "FieldValues": [

                {

                    "Field": {

                        "ArtifactID": 5435435,

                        "FieldCategory": "Generic",

                        "FieldType": "FixedLengthText",

                        "Guids": [],

                        "Name": "MarketNumber",

                        "ViewFieldID": 222344

                    },

                    "Value": [

                        "REL0000000132",

                        "REL0000000133"

                    ]

                },

                {

                    "Field": {

                        "ArtifactID": 1342343,

                        "FieldCategory": "Generic",

                        "FieldType": "SingleChoice",

                        "Guids": [],

                        "Name": "Recipient",

                        "ViewFieldID": 2222

                    },

                    "Value": [

                        {

                            "ArtifactID": 434355,

                            "Guids": [

                                "MyplaceholderGUID"

                            ],

                            "Name": "Extended Email Address"

                        },

                        {

                            "ArtifactID": 2222222,

                            "Guids": [

                                "MyplaceholderGUID2"

                            ],

                            "Name": "Extended Email Address1"

                        }

                    ]

                },

                {

                    "Field": {

                        "ArtifactID": 111111,

                        "FieldCategory": "Generic",

                        "FieldType": "FixedLengthText",

                        "Guids": [],

                        "Name": "website",

                        "ViewFieldID": 1003245

                    },

                    "Value": "microsoft.com"

                }

            ],

            "ArtifactID": 190090,

            "Guids": []

        }

    ],

    "IDWindow": [],

    "CurrentStartIndex": 1,

    "ResultCount": 1,

    "ObjectType": {

        "ArtifactID": 19999,

        "Name": "Document",

        "Guids": [

            "MyGuidforDoc"

        ],

        "ArtifactTypeID": 10

    },

    "RankWindow": []

}

2026-03-13 15-51-13
Samuel Espinoza

So this is a problem but it happens more often that I like, one of the possible solution I found is 
handle the response as text, and then you can handle depending of the case different structures to deserialize in the action or in an upper layer, but for that you need to know which structures use.

I hope it helps

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