Rest sensor - 2 possible JSON formats returned - what to do

Spot is a Satellite tracker than has a JSON API and I have written a rest sensor to get the last 3 readings to then display the distance travelled, speed and altitude. This is working great.

The problem is…Spot in their wisdom, totally change the format of the json if the unit hasn’t been turned on in a week and consider it an error. This then fills my log full of errors stating that the json is incorrect.

Home Assistant displays Unknown when this happens so handles it well, I just have an error every 90 seconds!

What would people suggest as a way to handle that the JSON returned may be one of two different structures?

  • Writing a custom component
  • Smart templating to determine the format of the JSON.

The “error” JSON is:
{“response”:{“errors”:{“error”:{“code”:“E-0195”,“text”:“No Messages to display”,“description”:“No displayable messages found found for feed: 0MKoxclifaMnB7WD0vYXaI63EJ8HX88xv”}}}}

JSON with message is:

{"response":{"feedMessageResponse":{"count":50,"feed":{"id":"0flo88TZq1QxCsRgr2kNUFlj2HFl5pVPO","name":"Phillip - Public Page","description":"Phillip - Public Page","status":"ACTIVE","usage":0,"daysRange":7,"detailedMessageShown":false,"type":"SHARED_PAGE"},"totalCount":94,"activityCount":0,"messages":{"message":[{"@clientUnixTime":"0","id":936558640,"messengerId":"0-8325883","messengerName":"PhilM","unixTime":1521182079,"messageType":"OK","latitude":-32.01584,"longitude":149.39746,"modelId":"SPOT2","showCustomMsg":"N","dateTime":"2018-03-16T06:34:39+0000","messageDetail":"","batteryState":"GOOD","hidden":0,"messageContent":"I have landed safely.","altitude":0},{"@clientUnixTime":"0","id":936557873,"messengerId":"0-8325883","messengerName":"PhilM","unixTime":1521181867,"messageType":"TRACK","latitude":-32.01489,"longitude":149.39796,"modelId":"SPOT2","showCustomMsg":"N","dateTime":"2018-03-16T06:31:07+0000","messageDetail":"","batteryState":"GOOD","hidden":0,"altitude":0},{"@clientUnixTime":"0","id":936555419,"messengerId":"0-8325883","messengerName":"PhilM","unixTime":1521181255,"messageType":"TRACK","latitude":-32.02548,"longitude":149.38531,"modelId":"SPOT2","showCustomMsg":"N","dateTime":"2018-03-16T06:20:55+0000","messageDetail":"","batteryState":"GOOD","hidden":0,"altitude":0}]}}}}