3

I have a few questions regarding using json file in ruby.

So my JSON file consists of the info below:

 {
  "2018-12-11": {
    "USD": 1.1379,
    "JPY": 128.75,
    "BGN": 1.9558,
    "CZK": 25.845,
    "DKK": 7.4641,
    "GBP": 0.90228,
    "HUF": 323.4,
    "PLN": 4.2983,
    "RON": 4.6557,
    "SEK": 10.297,
    "CHF": 1.1248,
    "ISK": 140.2,
    "NOK": 9.7038,
    "HRK": 7.3943,
    "RUB": 75.5225,
    "TRY": 6.1295,
    "AUD": 1.5778,
    "BRL": 4.4417,
    "CAD": 1.5239,
    "CNY": 7.8498,
    "HKD": 8.8934,
    "IDR": 16625.0,
    "ILS": 4.2691,
    "INR": 81.8215,
    "KRW": 1284.17,
    "MXN": 23.0209,
    "MYR": 4.7615,
    "NZD": 1.6526,
    "PHP": 60.009,
    "SGD": 1.5617,
    "THB": 37.317,
    "ZAR": 16.2903
  },
  "2018-12-10": {
    "USD": 1.1425,
    "JPY": 128.79,
    "BGN": 1.9558,
    "CZK": 25.866,
    "DKK": 7.4639,
    "GBP": 0.90245,
    "HUF": 323.15,
    "PLN": 4.2921,
    "RON": 4.6502,
    "SEK": 10.333,
    "CHF": 1.1295,
    "ISK": 140.0,
    "NOK": 9.6885,
    "HRK": 7.387,
    "RUB": 75.8985,
    "TRY": 6.0499,
    "AUD": 1.5842,
    "BRL": 4.4524,
    "CAD": 1.5218,
    "CNY": 7.8967,
    "HKD": 8.9283,
    "IDR": 16671.36,
    "ILS": 4.2642,
    "INR": 82.7284,
    "KRW": 1287.42,
    "MXN": 23.132,
    "MYR": 4.7671,
    "NZD": 1.6581,
    "PHP": 60.367,
    "SGD": 1.5667,
    "THB": 37.525,
    "ZAR": 16.3463
  }

There are a lot more dates in my json file. I have tried

require 'json'

file = File.read('eurofxref-hist-90d.json')

Parse Data from File.data_hash = JSON.parse(file)

I get an error

rb:7:in <main>': undefined methoddata_hash=' for File:Class (NoMethodError).

I am very new to Ruby and no idea what I am doing. Basically I want to get the information from the json file so I can develop a method that returns the exchange rate between from_currency and to_currency on date as a float.

5
  • 1
    Is Parse Data from File supposed to be a comment? If so you should put it on its own line and add a # to the start of the line. Otherwise Ruby thinks it's code Commented May 2, 2019 at 18:57
  • its suppose to be code not a comment Commented May 2, 2019 at 19:10
  • Parse Data from File. is not intended to be executable code in wherever you got this snippet from. Ruby thinks you are running this: Parse(Data(from(File.data_hash = JSON.parse(file)))) which calls a bunch of functions that don't exist. Commented May 2, 2019 at 19:11
  • It's a ruby hash with string keys. So data_hash["2018-12-11"]["USD"] for instance. Read about hashes here: docs.ruby-lang.org/en/2.0.0/Hash.html Commented May 2, 2019 at 19:18
  • 1
    Thanks Alex. This has been really helpful :) Commented May 2, 2019 at 19:20

2 Answers 2

6

Code provided by you is fine:

require 'json'
file = File.read('eurofxref-hist-90d.json')
data_hash = JSON.parse(file)

The problem with data, add closing bracket at the end:

{ "2018-12-11": { "USD": 1.1379, "JPY": 128.75, "BGN": 1.9558, "CZK": 25.845, "DKK": 7.4641, "GBP": 0.90228, "HUF": 323.4, "PLN": 4.2983, "RON": 4.6557, "SEK": 10.297, "CHF": 1.1248, "ISK": 140.2, "NOK": 9.7038, "HRK": 7.3943, "RUB": 75.5225, "TRY": 6.1295, "AUD": 1.5778, "BRL": 4.4417, "CAD": 1.5239, "CNY": 7.8498, "HKD": 8.8934, "IDR": 16625.0, "ILS": 4.2691, "INR": 81.8215, "KRW": 1284.17, "MXN": 23.0209, "MYR": 4.7615, "NZD": 1.6526, "PHP": 60.009, "SGD": 1.5617, "THB": 37.317, "ZAR": 16.2903 }, "2018-12-10": { "USD": 1.1425, "JPY": 128.79, "BGN": 1.9558, "CZK": 25.866, "DKK": 7.4639, "GBP": 0.90245, "HUF": 323.15, "PLN": 4.2921, "RON": 4.6502, "SEK": 10.333, "CHF": 1.1295, "ISK": 140.0, "NOK": 9.6885, "HRK": 7.387, "RUB": 75.8985, "TRY": 6.0499, "AUD": 1.5842, "BRL": 4.4524, "CAD": 1.5218, "CNY": 7.8967, "HKD": 8.9283, "IDR": 16671.36, "ILS": 4.2642, "INR": 82.7284, "KRW": 1287.42, "MXN": 23.132, "MYR": 4.7671, "NZD": 1.6581, "PHP": 60.367, "SGD": 1.5667, "THB": 37.525, "ZAR": 16.3463 } }
Sign up to request clarification or add additional context in comments.

6 Comments

this is not the whole data - there is quite a lot and I have checked the end and there is a closing bracket
ok then, try snippet that i've provided as mentioned by @alex-wayne Parse Data from File. - is comment that describes current step ;)
@CarySwoveland newlines don't have any impact except visual 👀
Its working now! thanks :) How do I access the values in the json file?
MrSnax, I never said that! In fact, I've not used JSON.
|
1

Think of this as an extended comment rather than an answer. (No upvotes, please.)

Your JSON string actually looks like this:

s=<<-END
{ "2018-12-11": { "USD": 1.1379, "JPY": 128.75, "BGN": 1.9558, "CZK": 25.845, "DKK": 7.4641, "GBP": 0.90228, "HUF": 323.4, "PLN": 4.2983, "RON": 4.6557, "SEK": 10.297, "CHF": 1.1248, "ISK": 140.2, "NOK": 9.7038, "HRK": 7.3943, "RUB": 75.5225, "TRY": 6.1295, "AUD": 1.5778, "BRL": 4.4417, "CAD": 1.5239, "CNY": 7.8498, "HKD": 8.8934, "IDR": 16625.0, "ILS": 4.2691, "INR": 81.8215, "KRW": 1284.17, "MXN": 23.0209, "MYR": 4.7615, "NZD": 1.6526, "PHP": 60.009, "SGD": 1.5617, "THB": 37.317, "ZAR": 16.2903 }, "2018-12-10": { "USD": 1.1425, "JPY": 128.79, "BGN": 1.9558, "CZK": 25.866, "DKK": 7.4639, "GBP": 0.90245, "HUF": 323.15, "PLN": 4.2921, "RON": 4.6502, "SEK": 10.333, "CHF": 1.1295, "ISK": 140.0, "NOK": 9.6885, "HRK": 7.387, "RUB": 75.8985, "TRY": 6.0499, "AUD": 1.5842, "BRL": 4.4524, "CAD": 1.5218, "CNY": 7.8967, "HKD": 8.9283, "IDR": 16671.36, "ILS": 4.2642, "INR": 82.7284, "KRW": 1287.42, "MXN": 23.132, "MYR": 4.7671, "NZD": 1.6581, "PHP": 60.367, "SGD": 1.5667, "THB": 37.525, "ZAR": 16.3463 } }
END
  #=> "    { \"2018-12-11\": { \"USD\": 1.1379, \"JPY\": 128.75, \"BGN\": 1.9558, \"CZK\": 25.845, \"DKK\": 7.4641, \"GBP\": 0.90228, \"HUF\": 323.4, \"PLN\": 4.2983, \"RON\": 4.6557, \"SEK\": 10.297, \"CHF\": 1.1248, \"ISK\": 140.2, \"NOK\": 9.7038, \"HRK\": 7.3943, \"RUB\": 75.5225, \"TRY\": 6.1295, \"AUD\": 1.5778, \"BRL\": 4.4417, \"CAD\": 1.5239, \"CNY\": 7.8498, \"HKD\": 8.8934, \"IDR\": 16625.0, \"ILS\": 4.2691, \"INR\": 81.8215, \"KRW\": 1284.17, \"MXN\": 23.0209, \"MYR\": 4.7615, \"NZD\": 1.6526, \"PHP\": 60.009, \"SGD\": 1.5617, \"THB\": 37.317, \"ZAR\": 16.2903 }, \"2018-12-10\": { \"USD\": 1.1425, \"JPY\": 128.79, \"BGN\": 1.9558, \"CZK\": 25.866, \"DKK\": 7.4639, \"GBP\": 0.90245, \"HUF\": 323.15, \"PLN\": 4.2921, \"RON\": 4.6502, \"SEK\": 10.333, \"CHF\": 1.1295, \"ISK\": 140.0, \"NOK\": 9.6885, \"HRK\": 7.387, \"RUB\": 75.8985, \"TRY\": 6.0499, \"AUD\": 1.5842, \"BRL\": 4.4524, \"CAD\": 1.5218, \"CNY\": 7.8967, \"HKD\": 8.9283, \"IDR\": 16671.36, \"ILS\": 4.2642, \"INR\": 82.7284, \"KRW\": 1287.42, \"MXN\": 23.132, \"MYR\": 4.7671, \"NZD\": 1.6581, \"PHP\": 60.367, \"SGD\": 1.5667, \"THB\": 37.525, \"ZAR\": 16.3463 } }\n"

Let's first write that string to a file.

FName = 'test'

File.write(FName, s)
  #=> 1013 (number of characters written)

Now we can read the file and convert the string to a hash:

require 'json'

JSON.parse(File.read(FName))
  #=> {"2018-12-11"=>{"USD"=>1.1379, "JPY"=>128.75, "BGN"=>1.9558, "CZK"=>25.845,
  #      "DKK"=>7.4641, "GBP"=>0.90228, "HUF"=>323.4, "PLN"=>4.2983, "RON"=>4.6557,
  #      "SEK"=>10.297, "CHF"=>1.1248, "ISK"=>140.2, "NOK"=>9.7038, "HRK"=>7.3943,
  #      "RUB"=>75.5225, "TRY"=>6.1295, "AUD"=>1.5778, "BRL"=>4.4417, "CAD"=>1.5239,
  #      "CNY"=>7.8498, "HKD"=>8.8934, "IDR"=>16625.0, "ILS"=>4.2691, "INR"=>81.8215,
  #      "KRW"=>1284.17, "MXN"=>23.0209, "MYR"=>4.7615, "NZD"=>1.6526, "PHP"=>60.009,
  #      "SGD"=>1.5617, "THB"=>37.317, "ZAR"=>16.2903},
  #    "2018-12-10"=>{"USD"=>1.1425, "JPY"=>128.79, "BGN"=>1.9558, "CZK"=>25.866,
  #      "DKK"=>7.4639, "GBP"=>0.90245, "HUF"=>323.15, "PLN"=>4.2921, "RON"=>4.6502,
  #      "SEK"=>10.333, "CHF"=>1.1295, "ISK"=>140.0, "NOK"=>9.6885, "HRK"=>7.387,
  #      "RUB"=>75.8985, "TRY"=>6.0499, "AUD"=>1.5842, "BRL"=>4.4524, "CAD"=>1.5218,
  #      "CNY"=>7.8967, "HKD"=>8.9283, "IDR"=>16671.36, "ILS"=>4.2642, "INR"=>82.7284,
  #      "KRW"=>1287.42, "MXN"=>23.132, "MYR"=>4.7671, "NZD"=>1.6581, "PHP"=>60.367,
  #      "SGD"=>1.5667, "THB"=>37.525, "ZAR"=>16.3463}} 

Note that

s=<<-END
...
END

is a heredoc. It's a convenient way of constructing a long string that contains quotes.

Comments

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.