title: Convert JSON Epoch to DateTime in Ruby for Conversion to CSV File
body: <p>Hello Stack Overflow Friends,</p>
  
  <p>We use an email metric API that returns data requested in JSON. What I am attempting to do is convert the JSON to a CSV file in Ruby (which I have been able to successfully do using the <a href="https://github.com/cescue/json-to-csv" rel="nofollow noreferrer">json-converter gem</a>). But, obviously JSON returns date/time fields in Epoch which is useless to most people. Therefore, I'm trying to figure out 2 things:</p>
  
  <p>1) How to appropriately convert Epoch into a more human-readable date/time for the CSV file, and</p>
  
  <p>2) Where along the way in my Ruby code do I need to attempt to do this - before the JSON to CSV conversion or after?</p>
  
  <p>Here's the sample code so far:</p>
  
  <p><strong>JSON Response</strong></p>
  
  <pre><code>{
    "data": [
      {
        "id": 571823,
        "subCommunityId": 2,
        "emailName": "Email Name",
        "fromName": "John Doe",
        "fromAddress": "john@doe.com",
        "subjectLine": "This is my subject",
        "preHeader": "",
        "categoryName": "General Communication",
        "sentCount": 15678,
        "scheduledDateTimestamp": 1504889858000,
        "actualSendTimestamp": 1504889858000,
        "dateAdded": 1504889859576
      }
    ],
    "startAt": 0,
    "maxResults": 1000,
    "total": 1
  }
  </code></pre>
  
  <p><strong>Ruby Script - JSON to CSV</strong></p>
  
  <pre><code>require 'json_converter'
  converter = JsonConverter.new
  json = File.read("emailresponse.json")
  converter.write_to_csv(json, "emailreporting.csv")
  </code></pre>
  
  <p>Any thoughts on how best to do this are welcome.</p>