Try to encode error message in json before attempting to parse it as xml

This is based on pull request #108 but honours that self._extract_error will fail if it tries to parse a json encoded error return.