user2909180
user2909180

Reputation: 215

Print out elements in a hash table, Ruby

I have a hash table that looks like this:

hash =
"{\"url\":\"/system/message\",\"device\":\"UNKNOWN\",\"version\":\"1.0\",\"timestamp\":\"2018-08-28T11:16:29.516617Z\",\"object\":{\"timestamp\":\"2018-08-28T11:16:29.516490Z\",\"id\":9800,\"debug_level\":2,\"message\":\"Got new port configuration\"}}"

 hash.each do |variable|
    puts variable
 end

This do not work

Upvotes: 0

Views: 247

Answers (2)

aditi banerjee
aditi banerjee

Reputation: 11

You can also do the following:

require 'json'
values = JSON.parse(hash).map {|k,v| puts v }

Upvotes: 0

dkulkarni
dkulkarni

Reputation: 2830

You'll need to first convert your string to a hash.

require 'json'

my_hash = JSON.parse(hash)
my_hash.each do |key, value|
  puts value
end

Upvotes: 4

Related Questions