AmirolAhmad AmirolAhmad - 27 days ago 20
Ruby Question

Ruby return value inside of hash array

When in my ruby console,

a = first
is

=> <Ng::EntityConfiguration id: 15881, entity_id: 1, entity_type: "Ng::Company", key: "wpa2.psk", value: "[{"ssid":"LVL6-Staff","password":"987654321", created_at: "2016-11-08 05:13:04", updated_at: "2016-11-08 05:13:04", name: "WIFI/Level 6">


then
a.value


will give you
=> "[{"ssid":"LVL6-Staff","password":"987654321","dhcp":"Enabled"}]"


My question is, how to get the value when object is Password? because when I try with
a.value[:password]
got an error.

Answer

It is a JSON string, after JSON.parse() is an array with hashes.

>> x = JSON.parse("[{\"ssid\":\"LVL6-Staff\",\"password\":\"987654321\",\"dhcp\":\"Enabled\"}]")
=> [{"ssid"=>"LVL6-Staff", "password"=>"987654321", "dhcp"=>"Enabled"}]
>> x.last['password']
=> "987654321"
Comments