Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.parse throwing Encoding::UndefinedConversionError

Tags:

ruby

encoding

Getting the following error on windows:

Encoding::UndefinedConversionError: "\xEF" from ASCII-8BIT to UTF-8

Code:

text = File.open(file, 'r:binary', &:read); #opens file and reads it with r:binary flag
puts text; #works i get here, outputs the below file contents
data = JSON.parse(text.force_encoding(Encoding::UTF_8)); #fails here with above error

Note: I've tried R:UTF-8 as well.


File Contents:

{
  "Environments": [
    {
      "Environment": "UT",
      "Configuration_Directory": "configs/",
      "Files": [
        {
          "Source": "Web.ENV.config",
          "Destination": "Web.config"
        }
      ]
    },
    {
      "Environment": "UAT",
      "Configuration_Directory": "configs/",
      "Files": [
        {
          "Source": "Web.ENV.config",
          "Destination": "Web.config"
        }
      ]
    },
    {
      "Environment": "Staging",
      "Configuration_Directory": "configs/",
      "Files": [
        {
          "Source": "Web.ENV.config",
          "Destination": "Web.config"
        }
      ]
    },
    {
      "Environment": "Production",
      "Configuration_Directory": "configs/",
      "Files": [
        {
          "Source": "Web.ENV.config",
          "Destination": "Web.config"
        }
      ]
    }
  ]
}
like image 992
abc123 Avatar asked Sep 26 '14 19:09

abc123


1 Answers

I had this problem where the original string was UTF-8 with BOM but Ruby had encoded it as ASCII-8bit. I convert the string to a byte array and then back into a string while forcing the encoding to be UTF-8.

string_value.bytes.pack("c*").force_encoding("UTF-8")
like image 85
Eric Parshall Avatar answered Nov 19 '22 00:11

Eric Parshall