Search code examples
mysqlrubyarraysjsonsequel

Can't Convert String into Integer - Extract Values from Hash within a Hash


I've found a bunch of topics that were very similar, but I'm just missing something. >.<

Basically, I'm trying to create the a variable called $db_url with the database credentials, host address, and name.

When trying to extract the value of a hash within a hash, I'm getting the following error:

`[]': can't convert String into Integer (TypeError)

on this line:

$credentials = $svc_details["credentials"]

Here's the context:

if (ENV['VCAP_SERVICES'])

  $vcap = JSON.parse(ENV['VCAP_SERVICES'])
  $svc_details = $vcap["mysql-5.1"]
  $credentials = $svc_details["credentials"]
  $host = $credentials["host"]
  $username = $credentials["username"]
  $password = $credentials["password"]
  $database = $credentials["name"]
  $port = $credentials["port"]
  $db_url = "mysql://#{$username}:#{$password}@#{$host}/#{$database}"

end

configure do
  Sequel.connect($db_url || ENV['DATABASE_URL'] || 'sqlite://blog.db')

  require 'ostruct'
  Blog = OpenStruct.new(
    :title => 'My Title',
    :author => 'My Name',
    :url_base => ENV['SITE_URL'],
    :admin_password => 'My Password',
    :admin_cookie_key => 'cookie_key',
    :admin_cookie_value => 'cookie_value',
    :disqus_shortname => nil
  )
end

EDIT:

Here's an example of the JSON I'm trying to work:

{"mysql-5.1":[
    {
        "name":"mysql-4f700",
        "label":"mysql-5.1",
        "plan":"free",
        "tags":["mysql","mysql-5.1","relational"],
        "credentials":{
            "name":"d6d665aa69817406d8901cd145e05e3c6",
            "hostname":"mysql-node01.us-east-1.aws.af.cm",
            "host":"mysql-node01.us-east-1.aws.af.cm",
            "port":3306,
            "user":"uB7CoL4Hxv9Ny",
            "username":"uB7CoL4Hxv9Ny",
            "password":"pzAx0iaOp2yKB"
        }
    },
    {
        "name":"mysql-f1a13",
        "label":"mysql-5.1",
        "plan":"free",
        "tags":["mysql","mysql-5.1","relational"],
        "credentials":{
            "name":"db777ab9da32047d99dd6cdae3aafebda",
            "hostname":"mysql-node01.us-east-1.aws.af.cm",
            "host":"mysql-node01.us-east-1.aws.af.cm",
            "port":3306,
            "user":"uJHApvZF6JBqT",
            "username":"uJHApvZF6JBqT",
            "password":"p146KmfkqGYmi"
        }
    }
]}

I'm a programming newbie, so I apologize if I'm missing information or don't understand something.


Solution

  • The can't convert String into Integer (TypeError) error is because $svc_details is an array i.e. a list of entries where you use a numeric index to select the one you want but you're trying to use a string "credentials" as an index for the array.

    If you look at your JSON you'll see that "mysql-5.1" at the start refers to an array (enclosed by the [] brackets) with 2 entries in it: the one that begins { "name":"mysql-4f700"... and the second one that begins { "name":"mysql-f1a13"...

    This means that when you write:

    $svc_details = $vcap["mysql-5.1"]
    

    then $svc_details is an array with 2 elements so you can't go straight to the "credentials"

    If you know whether you want the first or second entry then you can use $svc_details = $vcap["mysql-5.1"][0] or $svc_details = $vcap["mysql-5.1"][1] to select the appropriate section of the JSON, or you could write some code to find the entry with a particular name.