0

I have data in json format that I'm having troubles converting to DateTime to use within my rails app.

json data

{"status": "ok", "data": [{"2014-06-16 16:00:00": 24.2},{"2014-06-17 12:00:00": 30.2},{"2014-06-18 17:00:00": 42.9}]} etc 

Controller

@data = JSON.parse(open(@source.url).read) dates = [] temps = [] @data['data'].each do |data| dates << data.keys temps << data.values end datetime = DateTime.parse(dates).strftime("%Q") 

Gives me

can't convert Array into String 
1
  • I'm trying to convert into date time to use with Highcharts.CommentedAug 21, 2014 at 1:43

1 Answer 1

4

You are trying to parse an array not a string, because dates is an array. Parse each of the dates separately:

dates.flatten.each do |date| datetime = DateTime.parse(date) end 
4
  • 1
    This works but it's going to leave you with datetime containing only the last date in the array. That doesn't seem useful but it's also not clear what end result the question is looking for. Maybe use map to create an array of all the dates as DateTimes?
    – Jonah
    CommentedAug 21, 2014 at 1:39
  • @jonah, no, datetime will not even be set outside the each block. You must do something with it inside the block.
    – infused
    CommentedAug 21, 2014 at 1:42
  • @user3373464, I see why. You are pushing arrays into dates, so dates is an array of arrays. Try dates.flatten.each...
    – infused
    CommentedAug 21, 2014 at 1:43
  • Got it! Had to : datetime << DateTime.parse(date) thanks for the help.CommentedAug 21, 2014 at 2:31

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.