Consume REST: eliminate trailing comma in JSON

Consume REST: eliminate trailing comma in JSON

  
Hi everyone,

I am consuming a REST service that doesn't have the right JSON syntax response - it always puts a trailing comma on the last name/value pair, like this:
{
 "results":{
 "status":"OK",
 "items":[
  {
   "exchangecode":"NYI",
   "exchangedescription":"New York ISO",
  },
  {
   "exchangecode":"NYM",
   "exchangedescription":"NYMEX - New York Mercantile Exchange",
  },
 null
  ]
 }
}

The syntax error is highlighted in yellow. In order to fix this, I will have to create a "On After Response" action to manipulate that response, right? Any suggestion on how can I strip that trailing comma?

By the way, I know I can create an extension (.NET in this case) to do that - I think it's the easiest to do... Without an extension how could I solve this?

Thanks.
Hi Tiago,

Did you try with the Text extension and using a regex for it?
Something like a replace for  """,\s*\r?\n?\s*}" by """}"

Regards,
João Rosado
Hi João,

That is a good idea. I used those, with MultiLine set to True but didn't work.
However I decided to use my own extension with OnAfterResponseAdvanced and it worked with this:

RestResponse context = RestResponse.GetCurrent();
 
string removeMV = context.GetResponseBodyAsText();
 
var regex = new Regex(@"(.*?),\s*(\}|\])", (RegexOptions.Multiline));
 
var cleanJson = regex.Replace(removeMV, m => String.Format("{0} {1}", m.Groups[1].Value, m.Groups[2].Value));
 
context.SetResponseBody(cleanJson);

Could I achieve that Replace with the Text extension?
I remember another thing, at least one of the queries breaks because the provider was smart enough to put some double quotes inside the value... Double quotes inside double quotes in a Json string, smart :|  That should be fixed by the provider right? (they said they will fix the trailing comma)

Anyway, I noticed an interesting thing: even with that trailing comma OutSystems will output the correct data into the response structures.
Is OutSystems doing some processing on that response to handle that incorrect Json syntax?