Deseriliazing JSON array without predefining attributes

Deseriliazing JSON array without predefining attributes


OutSystems 10

I'm trying to build a structure in OutSytems to receive a dictionary from a C#.NET webservice call, but I can't seem to do it without predefining the attributes.  For example:

{"Data": [

{ "name": "FirstField", "value": "FirstValue" },

{ "name": "SecondField", "value": "SecondValue" }


The above works fine, creates a list of Name / Value pairs.  Great.  But this is not how C# naturally serializes the JSON, per our other developer who knows C# and JSON much better than I do.

However, the JSON received is coming through like this:

{"Data": {

"FirstField": "FirstValue",

"SecondField": "SecondValue"


To construct the structure in OutSystems off the second JSON, it creates a record with FirstField and SecondField as the named attributes.  This is not what we want, because we do not want to predefine the attributes.  EDIT:  we don't want to predefine because we don't necessarily know in our case what the field names are going to be.

Is there a way to do this array structure (native to C# dictionaries) in OutSystems?  I am at a loss after a week of research and trials.


Hello ChrisV,

Well, what we want and what we can do not always walk together... :)
OutSystems is not C# and do things differently.

In C#, in order to deserialize a JSON string without knowing the structure, it must be construct using reflection. In this case, the object will be created dynamically. But work with objects in this way is not trivial, even in C#, and requer extra coding.

In OutSystems, in order to deserialize a JSON string, the system needs to map the jason attributes to the structure attributes. There is no reflection in OutSystems. The structure MUST be know at design time, and the mapping (structure attribute's JSON property name) also. 

This means that you will never be able to get a JSON structure and map it dynamically to a structure in OutSystems code, nor will you be able to create a structure dynamically, to match the JSON string.

But there is a workaround, if the structure itself is really a dictionary (key:value) and the name of the json attributes are not important (just the values). As you are consuming a Rest, you can pre-process the JSON file and change it to match your structure, using OnAfterResponse. This way, you can receive whatever you want and change it to your structure.

Other possibility is, in this action OnAfterResponse, pass to a C# extension (built by you) the JSON and do this preprocessing in C#, if it is easier, returning the JSON as OutSystems will understand.

One way or another, the JSON must match a known structure, or you method will fail. (as far as I know).

Eduardo Jauch