It is well known that it is possible to convert a JSON dictionary to an object using Apex. But as I'm trying to do so with a JSON that is returned from a webservice, I'm facing an issue while trying to convert the data to object.
The problem is that I'm having to connect to a legacy webserver that only returns XML, and I have no access to modify it. So to get it as JSON I built a webservice that acts as a bridge between Salesforce and the original webservice. Salesforce calls the bridge, the bridge calls the webservice, the webservice returns XML to the bridge, and the bridge converts the XML to JSON and returns the JSON to Salesforce.
In a certain part of my XML I get a structure like the following:
<activities>
<activity>activity description here 1</activity>
<activity>activity description here 2</activity>
<activity>activity description here 3</activity>
</activities>
Seems only logical that when this is translated to JSON, it becomes something like this:
"activities": {
"activity": [
"activity description here 1",
"activity description here 2",
"activity description here 3"
]
}
However, sometimes the legacy webservice might return a single "activity", like:
<activities>
<activity>activity description here 1</activity>
</activities>
And then the JSON loses its list syntax, and becomes:
"activities" {
"activity" : "activity description here 1"
}
Because of this, I face errors when deserializing the class. Because Salesforce doesn't know when to use a list of values or not. I created the class to deserialize this JSON to, and I cannot use List<String> activities
, because it simply doesn't work when a single element is retrieved.
Has anyone faced an issue like this before? How can I solve this problem?
activity
anObject
instead of aString
orList<String>
. How are you consuming the data?JSON.deserializeUntyped
will work for me.