TestingInProd
TestingInProd

Reputation: 379

Ruby : Convert timestamp to date time in given json input

I have a JSON input. I would like to convert all timestamp (createdDate ,modifiedDate) to time in ruby. How do I do that? I tried below methods but dint work

characterList.each { |char| DateTime.strptime(char.try(:getEditInfo).try(:getCreatedDate),%s) }


{"characterList": 
  [
    {"editInfo": 
      {"createdBy": "testname", 
       "createdDate": 1503137795000, 
       "modifiedBy": "testname", 
       "modifiedDate": 1503137795000}, 
     "charInfo": 
      {"charid": "3434", 
       "charDesc": "3434", 
       "status": "ON"}
    }, 
    {"editInfo": 
      {"createdBy": "testname", 
       "createdDate": 1503137795000, 
       "modifiedBy": "testname", 
       "modifiedDate": 1503137795000}, 
     "charInfo": 
      {"charid": "3434   6", 
       "charDesc": "43dfdf", 
       "status": "ON"}
    }, 
    {"editInfo": 
      {"createdBy": "testname", 
       "createdDate": 1503137795000, 
       "modifiedBy": "testname", 
       "modifiedDate": 1503137795000}, 
     "charInfo": 
      {"charid": "4hr_SLA", 
       "charDesc": "sd", 
       "status": "ON"}
    }, 
    {"editInfo": 
      {"createdBy": "testname", 
       "createdDate": 1503137795000, 
       "modifiedBy": "testname", 
       "modifiedDate": 1503137795000}, 
     "charInfo": 
      {"charid": "aaaaaaaaaa", 
       "charDesc": "asdfaadsf   asdfasdf asdf", 
       "status": "ON"}
    }, 
    {"editInfo": 
      {"createdBy": "testname", 
       "createdDate": 1503137795000, 
       "modifiedBy": "testname", 
       "modifiedDate": 1503137795000}, 
     "charInfo": 
      {"charid": "abababab", 
       "charDesc": "abababababab", 
       "status": "ON"}
    }
  ]} 

I am ok converting in 2 like separately for createdDate and modifiedDate. But Im looking for one line solution

Upvotes: 0

Views: 588

Answers (1)

Simple Lime
Simple Lime

Reputation: 11035

You can do something like:

json[:characterList].map! do |character|
  character.tap do |char|
    if editInfo = char[:editInfo]
      if editInfo[:createdDate]
        editInfo[:createdDate] =
          DateTime.strptime(editInfo[:createdDate].to_s, '%Q')
      end

      if editInfo[:modifiedDate]
        editInfo[:modifiedDate] =_
          DateTime.strptime(editInfo[:modifiedDate].to_s, '%Q')
      end
    end
  end
end

the main issues with your code were

  1. No :getEditInfo or :getCreatedDate, though, those may just be methods you made but didn't include here.
  2. strptime takes a string, the *Date fields are integers
  3. The %s argument to strptime is the number of seconds since 1970-01-01 00:00:00 UTC, I'm going to assume you aren't dealing with dates in the year 49,602, so changed it to %Q, which is the number of milliseconds since 1970-01-01 00:00:00 UTC.

This is just a sample, modifying the JSON hash itself with DateTime objects, you may need to be doing something different with it, the big thing here is the %Q and to_s in strptimes

DateTime.strptime(1503137795000.to_s, '%Q') # => 2017-08-19T10:16:35+00:00

Without knowing what's actually wrong with your code ("didn't work" isn't very descriptive), if everything else on that line you tried work you could just change to:

characterList.each { |char| DateTime.strptime(char.try(:getEditInfo).try(:getCreatedDate).to_s,'%Q') }

for a one-liner

Upvotes: 1

Related Questions