I have a file text.json and I have an JSON HTTP response. What's a good to check if they are equal?
Here's what I have but I think there's a better solutions.
JSON.parse(response["data"]).eql?(File.read(text.json))
You need to parse both ends of your test:
JSON.parse(response["data"]).eql?(JSON.parse(File.read(text.json)))
Edit
If you want to test an array of JSONs, and you are not sure that the order will be the same in the file meaning [{a:1, b:2}, {a:2, b:1}]
should equal [{a:2, b:1}, {a:1, b:2}]
, you'll need to sort them first (see here for more techniques):
JSON.parse(response["data"]).sort.eql?(JSON.parse(File.read(text.json)).sort)
Edit 2
Since Hash
es don't sort well, the above won't work. You could use one of the other techniques:
from_response = JSON.parse(response["data"])
from_file = JSON.parse(File.read(text.json))
(from_response & from_file) == from_response
(from_response - from_file).empty?