0

I'm trying to get daily values from google rate exchange API (http://rate-exchange.appspot.com/currency?from=USD&to=EUR) and write the values to a JSON file. What I have so far is:

quote = agent.get("http://rate-exchange.appspot.com/currency?from=USD&to=EUR").body
parsed = JSON.parse(quote)
stored = {:date => Time.now, :rate => parsed['rate']}
File.open("ruby_test.json", "w") { |f| f.write(JSON.pretty_generate(stored)) }

But this will overwrite my json file everytime I run it, the error is the way I am creating the ruby hash or the way I'm writing the json file?

1 Answer 1

1

To append to the file, open it with a file open mode of a instead of w:

File.open("ruby_test.json", "a") { |f| f.write(JSON.pretty_generate(stored)) }
Sign up to request clarification or add additional context in comments.

1 Comment

But this will give me an invalid json file, I think this way I'm just appending two strings together

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.