Ruby: How to Convert an Array of Data to Hash and to JSON Format

Ruby: How can I convert an array of data to hash and to json format?

array.map { |o| Hash[o.each_pair.to_a] }.to_json

Ruby converting array to hash object to json

I suggest you use the method Array#product.

{ "cars": ["id"].product(ids).map { |k,v| { k=>v } } }
#=> {:cars=>[{"id"=>111}, {"id"=>333}, {"id"=>888}]}

or

{ "cars": ["id"].product(ids).map { |a| [a].to_h } }
#=> {:cars=>[{"id"=>111}, {"id"=>333}, {"id"=>888}]}

and then apply to_json.

Format array of date_time in hash during hash to json conversion

You cannot make this part of Hash#to_json without damaging that method dramatically because:

  • You would need to manipulate the #to_json for multiple other classes
  • Those are Integers which is valid JSON and changing this would be awful
  • That is not the string representation of a Time object in Ruby so you need to string format it anyway

Instead you would have to modify the Hash values to represent in the desired fashion e.g.

h= {"dateTime"=>[1484719381, 14848723546], "dateTime1"=>[1484234567, 1484719381]}
h.transform_values do |v|
v.map do |int|
Time.at(int, in: '+00:00').strftime("%Y-%m-%dT%H:%M:%S%z")
end
end
#=> {"dateTime"=>[
# "2017-01-18T06:03:01+0000",
# "2440-07-15T05:25:46+0000"],
# "dateTime1"=>[
# "2017-01-12T15:22:47+0000",
# "2017-01-18T06:03:01+0000"]}

You could then call to_json on the resulting object to get your desired result.

Array of Hashes to JSON File Ruby

The problem is that you pack each rule inside an array :

JSON.pretty_generate([rule])

You should create an array of matching rules, and dump it as JSON :

entities.each do |entity|
File.open(File.join(path, entity.name + file_name), "w") do |f|
matching_rules = array_hashes.select{ |rule| rule['rulename'].match(entity.name) }
f.write(JSON.pretty_generate(matching_rules))
end
end

Convert Array of Arrays into JSON

The output is correct. Curly brackets are around hashes, but your data attribute is a nested array.

If you want to convert a nested array into a hash, just call to_h on it:

{ :results => weekdays.count, :data => weekdays.to_h }

How to create a JSON Array from a two-dimensional array in Ruby

Your desired result is still a Ruby hash. To transform it call map on the array and create a hash for each entry:

a = [
["01fe237f804a5eff182dcded115c37d3", 0.0140845],
["026e5f1f7f026bf3c763523206aa44bf", 0.03448275],
["04a1c3c79773bd1ecc0372a991adc815", 0.04617604]
]

a.map{ |e| {address: e[0], value: e[1]} }

This returns the desired result.

If you want to create a JSON string, require json and do the following:

require 'json'

a = [
["01fe237f804a5eff182dcded115c37d3", 0.0140845],
["026e5f1f7f026bf3c763523206aa44bf", 0.03448275],
["04a1c3c79773bd1ecc0372a991adc815", 0.04617604]
]

a.map{|e| {address: e[0], value: e[1]} }.to_json

This will encode the result to the following string:

"[{\"address\":\"01fe237f804a5eff182dcded115c37d3\",\"value\":0.0140845},{\"address\":\"026e5f1f7f026bf3c763523206aa44bf\",\"value\":0.03448275},{\"address\":\"04a1c3c79773bd1ecc0372a991adc815\",\"value\":0.04617604}]"

Return specific values from array of hashes - JSON

yes, at the end, I find the solution. res is an array and there are multiple hashes in this array, As I'm getting this from Google Places API, so if someone wants to access the name, latitude, longitude, place_id of nearby restaurants he can do it easily by .Map by the following code.

resturants =res.map { |a| {id: a.place_id ,  name: a.name , lat: a.lat, lng: a.lng , icon: a.icon}}
json_response "Successfully retrive data" , true, {Resturant: resturants }, :ok

How to convert an array of ids to array of hash with a key in ruby?

Anything like this?

array.map { |id| Hash[:id, id] }

the same with hash literal

array.map { |id| { id: id } }


Related Topics



Leave a reply



Submit