Retro-fitted Chris's improved JSON error check code
(because I've no idea how to combine two commits from different boxes/users in Git)