title: ruby: Extracting fields from nested json
body: <p>I am trying to teach myself ruby and solve a problem at work. My ultimate goal is to extract out three of the many fields in JSON response from an API, manipulate and dump to CSV for executive reporting. </p>
  
  <p>The structure of the JSON is:</p>
  
  <pre><code>{
    "status": 200,
    "data": {
      "total": 251,
      "alerts": [
        {
          "dataPoint": "x",
          "ackedBy": "x",
          "dataSourceInstance": "x",
          "dataSource": "x",
          "host": "x",
          "endOn": 0,
          "ackedOn": 1385085190,
          "dataSourceInstanceId": 588384,
          "hostId": 935,
          "type": "alert",
          "dataSourceId": 694,
          "ackedOnLocal": "2013-11-21 17:53:10 PST",
          "id": 6170384,
          "startOn": 1385084917,
          "thresholds": "!= 1 1 1",
          "endOnLocal": "",
          "level": "error",
          "ackComment": "x",
          "value": "No Data",
          "hostDataSourceId": 211986,
          "acked": true,
          "hostGroups": [{
            "alertEnable": true,
            "createdOn": 1362084592,
            "id": 21,
            "parentId": 78,
            "description": "",
            "appliesTo": "",
            "name": "2600hz",
            "fullPath": "x"
          }],
          "startOnLocal": "2013-11-21 17:48:37 PST"
        },
  </code></pre>
  
  <p>To be specific I want to extract out <code>dataPoint</code>,<code>startOn</code>,<code>ackedOn</code>.</p>
  
  <p>I am thinking I need to extract the total value first, so I know how many alerts I have. That will help me loop through the alert instances.</p>
  
  <pre><code>*url = "https://xyz"
  uri = URI(url)
  http = Net::HTTP.new(uri.host, 443)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  req = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(req)
  
  # Make sure we had a proper web response
  if response.code == '200' then
  
  # Set up that the total is two levels deep in the json
  path = JsonPath.new('$.total')
  
  # Extract out total field into variable
  totalAlerts = path.on(response)
  
  # Print hash to confirm value
  pp totalAlerts
  
  end*
  </code></pre>
  
  <p>I am stuck trying to just extract out the total. Output shows nothing:</p>
  
  <pre><code>sudo ruby alerts.rb
  []
  </code></pre>
  
  <p>Is there a better way to do this?</p>