So I'm consuming a REST API that is provided by my Identity team to be able to query user data. Well I'm trying to get the member property. So if they are a member of many groups it returns as a string array in the JSON response, but if they are just a member of one group it returns as a single string property in the JSON response. Any one know how I could convert the single case to an string array so I'm always getting the list? I have asked the REST owner and they are not able to change the behavior so I need to handle it on the response side before OS tries to convert to a structure.
String Array JSON
{"resources": [{"dn": "objectid=c22b951a-bf2c-40e7-bf71-d5b8786edc80", "attributes": {"member": ["objectid=fa4f5bbf-f795-4607-8135-8a6df072686a", "objectid=ef7ecf0f-9603-4b55-b29c-67dc01a44c8a", "objectid=0f211cbf-a526-4139-89c2-146d0b457a38", "objectid=ee546aba-dc80-4312-b399-21183befb68f", "objectid=23e9ce31-7ed1-48fa-ba96-9776ca8ac98b", "objectid=bbfa54d3-91ef-4ed9-b875-2897028aa241", "objectid=996ddaa5-0c2b-4e9f-bb11-c0954caf65e0", "objectid=1a6431e3-c430-4207-830c-14cd9819ed84", "objectid=c0654cbb-eb0c-4629-b1a1-0997c0f49587]}}], "totalResults": -2, "count": 1}
Single string JSON
{"resources": [{"dn": "objectid=40468e12-8251-400a-b358-a6e9926d33ed", "attributes": {"member": "objectid=c22b951a-bf2c-40e7-bf71-d5b8786edc80"}}], "totalResults": -2, "count": 1}
Hi Jeremy,
if you are dead set on handling it before OS deserializes it, you would need to handle in an OnAfterResponse. Should be fairly straightforward with a regex replace. What i don't like about it, is that you can't define an OnAfterResponse per api method, but if your consumed api only has the one method, that's a non-issue.
But maybe an alternative is to have both options in your structure, and then in some wrapper action that you have around the rest call, add the single one to the (empty) list, so the rest of your outsystems application only ever has to deal with a list.
Dorine
EDIT : i see that both responses are called 'member', so option 2 is out, i guess
I agree and I don't really want to have to handle it in the AfterResponse, but as you stated, they are both the same property in JSON so I can't solve it in the structure. But you did give me an idea. I could change the name of the property to something else and put that in the structure instead of trying to convert the data to a list with regex. So if I detect it is single, then I could just change the property name to something like SingleMember and put that in object. I knew I just need to talk it out. Thanks.
yes, that'll do the trick
It worked. Checked if the property was in the JSON first and then checked if it was an Array or not. If not an Array then I modified the ResponseText to change the property name. Thanks for the idea.