Reputation: 1314
The scenario is like so: I have a directory which has multiple JSON files which have similar data, but not the same exact data (the structure is the same, but the data may not not necessarily be the same).
I need to find the keys which are similar between all the JSON files (i.e the intersection of all the JSON files).
I load the JSON files like so
require 'json'
ARGV.each {|x|
JSON.parse(File.read(x))
}
From here, I don't know how to get the intersection of the hashes.
I know you can use sets, like so
require 'json'
require 'set'
ARGV.each {|x|
JSON.parse(File.read(x)).to_set
}.reduce(:&)
But as per this post Hashes Vs. Set Performace, hashes seem to be faster (although I guess it depends the use cases)
So, how can I find the intersection of multiple hashes (where the key value pairs are the same), without using Set?
Upvotes: 0
Views: 227
Reputation: 12558
You don't need to use a set. A set maintains that all elements are unique, and a JSON hash will never have two identical key-value pairs. I'd just use a normal array (to_a
).
One problem is that you are actually calling reduce(:&)
on ARGV
, instead of the parsed JSON. You can change each
to map
to fix this:
ARGV.map { |x|
JSON.parse(File.read(x)).to_a
}.reduce(:&)
If you want to convert this back into a hash form, you can use to_h
.
Upvotes: 1