Search code examples
ruby-on-railsjsonrubyruby-on-rails-4jsonparser

Ruby on Rails JSON.parse "unexpected token" error


I am trying to parse JSON data in my Rails 4 application with JSON.parse. I keep getting this error:

757: unexpected token at '<?xml version="1.0" encoding="UTF-8"?> <Error><Code>AccessDenied</Code><Message>Access Denied</Message><RequestId>BB0CDD4F68DBE9C0</RequestId><HostId>SwLqpcx7wpspbYzXONm1XGRG46dGHxnDroplppsxqtU4v/iVwEGOvpRWpBNR+LMPduTldva/5Nk=</HostId></Error>'

enter image description here


Solution

  • Looks like error response is not returned as JSON. You could do something like:

    def reviews
      JSON.parse(reviews_json)['Reviews']
    rescue JSON::ParserError
      []
    end