0

I have a two-dimensional array like this:

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

I'd like to create a hash first, then convert it to JSON, so that the result looks like the following:

[{address: "01fe237f804a5eff182dcded115c37d3", value: 0.0140845},
{address: "026e5f1f7f026bf3c763523206aa44bf", value: 0.03448275},
{address: "04a1c3c79773bd1ecc0372a991adc815", value: 0.04617604}]
4
  • 1. If you know that require ‘json’; arr.to_json converts an array arr to a JSON string you should have limited your question to the part you don’t understand: how to convert an array of two-element arrays to an array of hashes with specified keys. 2. Always simplify your examples. Here arr = [["01fe", 0.014], ["026e", 0.034], ["04a1", 0.046]] would have sufficed. 3. As I did in #2, assign a variable (e.g., ’arr`) to each of the example’s inputs so that readers can refer to those variables in answers and comments without having to define them... Commented Dec 27, 2019 at 17:24
  • ...4. Always show the desired result of an example as a valid Ruby object. You show what appears to be a string but strings are surrounded by double or single quotes. (If you add double quotes you need to escape the inner double quotes.) This may seem a quibble but some readers are fussy about that. 5. This is a pure-Ruby question so you should not have a Rails’ tag. 6. Your question is implicit (“How do I do that?”). Often that will provoke the comment, “What is your question?”. It’s best to always state your question... Commented Dec 27, 2019 at 17:41
  • ...7. The tags “array” and “hash” are probably unnecessary as it’s unlikely that such common objects would be used as search terms. Commented Dec 27, 2019 at 17:41
  • Welcome to SO! Please see the "Stack Overflow question checklist" and "How to Ask". We want to see your effort toward solving this. Did you research it? If so, where and why didn't it help? Did you write code? If so, what is the minimal code that demonstrates the problem you encountered, and what was that problem? Without that it looks like you didn't try and want us to write the code for you which is off-topic. Commented Dec 31, 2019 at 5:38

2 Answers 2

2

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}]"
Sign up to request clarification or add additional context in comments.

2 Comments

simple & neat +1, I have done with different approach
Some may prefer a.map { |v1,v2| { address: v1, value: v2 } }.to_json for readability.
1

You can do as below,

require 'json'
keys = [:address, :value]
arr.map { |values| Hash[keys.zip(values)] }.to_json

1 Comment

It's worth noting that Hash[keys.zip(values)] can be replaced with keys.zip(values).to_h, though I don't think either is preferred. Your answer has the advantage that if embeded in a method with arguments arr and keys, keys can be any size (as opposed to only working when there are two keys).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.