AmirolAhmad AmirolAhmad - 1 year ago 88
Ruby Question

Ruby return value inside of hash array

When in my ruby console,

a = first

=> <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">


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
got an error.

Answer Source

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"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download