Additional error trapping when encoding json data