REST Json sometimes object, sometimes array, sometimes nothing

REST Json sometimes object, sometimes array, sometimes nothing

  

I want to use a Rest-service that sometimes provides an object, sometimes an array and sometimes nothing at all. The problem is a REST-consuming-service in Outsystems expects a certain format

(For example "SaleLine", which contains a list of SaleLine Items, which then contain SalieLine Items.)

When the REST-service only has one SaleLine Item it doesn't return an array of SaleLine Items but just return the SaleLine Item

(For example "SaleLine", which contains a SalieLine Item.)...

Outsystems dies offcourse in a 500 error saying it cannot deserialize the current JSON object.

How can I solve this?


So the REST service you consume sometimes returns an array, and sometimes a single record, with the same name?

Yep:

So for example:

"SaleLines": {
   [
      "SaleLine":
        {
          "saleLineID": "109",
          "createTime": "2016-10-23T22:22:50+02:00",
  ...
        },
        {
          "saleLineID": "110",
          "createTime": "2016-10-23T22:23:22+02:00",
   ...
        },
        {
          "saleLineID": "111",
          "createTime": "2016-10-23T22:23:51+02:00",
  ...
        },
        {
          "saleLineID": "112",
          "createTime": "2016-10-23T22:24:16+02:00",
          ...
          }
        },
        {
          "saleLineID": "113",
          "createTime": "2016-10-23T22:24:26+02:00",
          ...
        }
      ]
    }


And next time:

"SaleLines": {
      "SaleLine":
        {
          "saleLineID": "109",
          "createTime": "2016-10-23T22:22:50+02:00",
            ...
        }

}

Blergh, that's ugly! I'm not sure how common this practice is, though I recall having seen it before. The only fix for OS I can think of is handling this situation in the OnAfterResponse, manually stuffing the extra [ and ] in the second example...

Thanks a lot @Kilian.

Based on some regex search then? :P --> looks dirty to me too... Any other idea or did someone face his before and solved it differently?

Yup, it'll definitely be dirty.