Good day,
I've been encountering this problem where an element from the Xml can either have one or more items inside it, so when I convert it into Json:
One item translates to { "name" : "value" } while multiple items translate to [ { "name" : "value" } ].
So, when I deserialize the Json, it returns an error since the Structure is set to List but is only returning one item.
Are there any work arounds for this for forcing the Element into becoming an Array before converting to Json?
Thank you in advance to anyone who will be able to answer.
Best Regards,
Jason
Hi Jason,
You should build a custom structure and use that rather than a list as it's the wrong data type.
More on how to that here: https://success.outsystems.com/Documentation/11/Developing_an_Application/Use_Data/Use_Structures_and_Records_to_Create_Compound_Data_Types
I hope this helps.
Hi @Jason Paul Alcantara ,
Approach 1 : detect if it is an array or not (some regex, I guess). If not, do some string manipulations adding the square brackets. Then always deserialize to a list.
Approach 2 : try to deserialize to list. If that fails, deserialize to single item and append it to the list.
Dorine
I agree with Dorine's approaches.
I don't think there's much to do.
Anyway, you need to try to tweak the json before deserialization.
Best regards
Tiago Vital
I have the same problem as Jason. Dorine's approach isn't feasible in my case, as we're talking many layers of nested structures, some of which are lists. Manipulation of the raw JSON would be just as much work as parsing the XML myself.
What would be ideal is if the conversion action would also allow optional WSDL and XSDs so the actual structure could be known, but I realise that's probably too much work and/or not compatible with the current library used.
Mmm, this is an interesting article about adding "json:Array" to an element. Could be feasible to add this optionally to the converter. I'll see if I can come up with something...
EDIT: That doesn't seem to work at all, at least with the current version of the library...
is the only problem you are facing that sometimes there is one of something and sometimes there are more, which results in sometimes a json object, sometimes a json array ?
If that is the only problem, you could define on OS structure side everything as list and you could do some manipulations on your xml / json to fix this (a bit dirty, but might be the thing you need, and might be possible to build once and use for everything.
can you share an example xml showing what kind of variations you need to deal with ?
I considered that, but we're talking about huge XML files with sometimes a structure like this:
and other times a structure like this (so more than one register):
I could theoretically manipulate the XML or resulting JSON, but it'd be quite some effort (note that the structures above are already a few levels deep).
Ok, I've uploaded a new version that can deal with the array business. It's now possible to indicate which nodes must be arrays in JSON, using this method.