ruby on rails - Update value in hash array with json.parse -
i have 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">
so when call a.value
, return => "[{"ssid":"lvl6-staff","password":"987654321","dhcp":"enabled"}]"
then, wanted value password:
x = json.parse(a.value) x.last['password'] => "987654321"
my question is, after password value, want update password value '123456789' , save it. how achieve this?
this should simple.
string = '[{"ssid":"lvl6-staff","password":"987654321","dhcp":"enabled"}]' # `a.value` json = json.parse(string) new_password = '123456' # or whatever json.first['password'] = new_password new_string = json.to_json # "[{\"ssid\":\"lvl6-staff\",\"password\":\"123456\",\"dhcp\":\"enabled\"}]"
Comments
Post a Comment