title: Add lines to a json file extracting key value pair using Python
body: <p><strong>Issue:</strong></p>
  <p>I have two lines in a raw json file which has several key value pairs. I'm trying to extract specific key value pairs for each line, append it to a dictionary and then dump the output as a new json file which has all the data I'm looking for --&gt;</p>
  <pre><code>{
      &quot;visitNumber&quot;: &quot;151&quot;,
      &quot;visitId&quot;: &quot;1470083489&quot;,
      &quot;visitStartTime&quot;: &quot;1470083489&quot;,
      &quot;date&quot;: &quot;20160801&quot;,
      &quot;totals&quot;: {
          &quot;visits&quot;: &quot;1&quot;,
          &quot;hits&quot;: &quot;3&quot;,
          &quot;pageviews&quot;: &quot;3&quot;,
          &quot;timeOnSite&quot;: &quot;529&quot;
      },
      &quot;device&quot;: {
          &quot;browser&quot;: &quot;Chrome&quot;,
          &quot;browserVersion&quot;: &quot;not available in demo dataset&quot;,
          &quot;browserSize&quot;: &quot;not available in demo dataset&quot;,
          &quot;operatingSystem&quot;: &quot;Windows&quot;,
          &quot;operatingSystemVersion&quot;: &quot;not available in demo dataset&quot;,
          &quot;isMobile&quot;: false,
          &quot;mobileDeviceBranding&quot;: &quot;not available in demo dataset&quot;,
          &quot;mobileDeviceModel&quot;: &quot;not available in demo dataset&quot;,
          &quot;mobileInputSelector&quot;: &quot;not available in demo dataset&quot;,
          &quot;mobileDeviceInfo&quot;: &quot;not available in demo dataset&quot;,
          &quot;mobileDeviceMarketingName&quot;: &quot;not available in demo dataset&quot;,
          &quot;flashVersion&quot;: &quot;not available in demo dataset&quot;,
          &quot;language&quot;: &quot;not available in demo dataset&quot;,
          &quot;screenColors&quot;: &quot;not available in demo dataset&quot;,
          &quot;screenResolution&quot;: &quot;not available in demo dataset&quot;,
          &quot;deviceCategory&quot;: &quot;desktop&quot;
      },
      &quot;geoNetwork&quot;: {
          &quot;continent&quot;: &quot;Americas&quot;,
          &quot;subContinent&quot;: &quot;Northern America&quot;,
          &quot;country&quot;: &quot;United States&quot;,
          &quot;region&quot;: &quot;California&quot;,
          &quot;metro&quot;: &quot;San Francisco-Oakland-San Jose CA&quot;,
          &quot;city&quot;: &quot;San Jose&quot;,
          &quot;cityId&quot;: &quot;not available in demo dataset&quot;,
          &quot;networkDomain&quot;: &quot;unknown.unknown&quot;,
          &quot;latitude&quot;: &quot;not available in demo dataset&quot;,
          &quot;longitude&quot;: &quot;not available in demo dataset&quot;,
          &quot;networkLocation&quot;: &quot;not available in demo dataset&quot;
      },
      &quot;hits&quot;: [
          {
              &quot;hitNumber&quot;: &quot;1&quot;,
              &quot;time&quot;: &quot;0&quot;,
              &quot;hour&quot;: &quot;13&quot;,
              &quot;minute&quot;: &quot;31&quot;,
              &quot;isInteraction&quot;: true,
              &quot;isEntrance&quot;: true,
              &quot;page&quot;: {
                  &quot;pagePath&quot;: &quot;/home&quot;,
                  &quot;hostname&quot;: &quot;shop.googlemerchandisestore.com&quot;,
                  &quot;pageTitle&quot;: &quot;Home&quot;,
                  &quot;pagePathLevel1&quot;: &quot;/home&quot;,
                  &quot;pagePathLevel2&quot;: &quot;&quot;,
                  &quot;pagePathLevel3&quot;: &quot;&quot;,
                  &quot;pagePathLevel4&quot;: &quot;&quot;
              }
          },
          {
              &quot;hitNumber&quot;: &quot;2&quot;,
              &quot;time&quot;: &quot;289206&quot;,
              &quot;hour&quot;: &quot;13&quot;,
              &quot;minute&quot;: &quot;36&quot;,
              &quot;isInteraction&quot;: true,
              &quot;page&quot;: {
                  &quot;pagePath&quot;: &quot;/google+redesign/apparel/men++s/men++s+t+shirts&quot;,
                  &quot;hostname&quot;: &quot;shop.googlemerchandisestore.com&quot;,
                  &quot;pageTitle&quot;: &quot;Men's-T-Shirts&quot;,
                  &quot;pagePathLevel1&quot;: &quot;/google+redesign/&quot;,
                  &quot;pagePathLevel2&quot;: &quot;/apparel/&quot;,
                  &quot;pagePathLevel3&quot;: &quot;/men++s/&quot;,
                  &quot;pagePathLevel4&quot;: &quot;/men++s+t+shirts&quot;
              }
          }
         ],
    }
  
  
  
  {
      &quot;visitNumber&quot;: &quot;1&quot;,
      &quot;visitId&quot;: &quot;1470117657&quot;,
      &quot;visitStartTime&quot;: &quot;1470117657&quot;,
      &quot;date&quot;: &quot;20160801&quot;,
      &quot;totals&quot;: {
          &quot;visits&quot;: &quot;1&quot;,
          &quot;hits&quot;: &quot;3&quot;,
          &quot;pageviews&quot;: &quot;3&quot;,
          &quot;timeOnSite&quot;: &quot;54&quot;,
          &quot;newVisits&quot;: &quot;1&quot;
      },
      &quot;device&quot;: {
          &quot;browser&quot;: &quot;Internet Explorer&quot;,
          &quot;browserVersion&quot;: &quot;not available in demo dataset&quot;,
          &quot;browserSize&quot;: &quot;not available in demo dataset&quot;,
          &quot;operatingSystem&quot;: &quot;Windows&quot;,
          &quot;operatingSystemVersion&quot;: &quot;not available in demo dataset&quot;,
          &quot;isMobile&quot;: false,
          &quot;mobileDeviceBranding&quot;: &quot;not available in demo dataset&quot;,
          &quot;mobileDeviceModel&quot;: &quot;not available in demo dataset&quot;,
          &quot;mobileInputSelector&quot;: &quot;not available in demo dataset&quot;,
          &quot;mobileDeviceInfo&quot;: &quot;not available in demo dataset&quot;,
          &quot;mobileDeviceMarketingName&quot;: &quot;not available in demo dataset&quot;,
          &quot;flashVersion&quot;: &quot;not available in demo dataset&quot;,
          &quot;language&quot;: &quot;not available in demo dataset&quot;,
          &quot;screenColors&quot;: &quot;not available in demo dataset&quot;,
          &quot;screenResolution&quot;: &quot;not available in demo dataset&quot;,
          &quot;deviceCategory&quot;: &quot;desktop&quot;
      },
      &quot;geoNetwork&quot;: {
          &quot;continent&quot;: &quot;Americas&quot;,
          &quot;subContinent&quot;: &quot;Northern America&quot;,
          &quot;country&quot;: &quot;United States&quot;,
          &quot;region&quot;: &quot;not available in demo dataset&quot;,
          &quot;metro&quot;: &quot;not available in demo dataset&quot;,
          &quot;city&quot;: &quot;not available in demo dataset&quot;,
          &quot;cityId&quot;: &quot;not available in demo dataset&quot;,
          &quot;networkDomain&quot;: &quot;telia.net&quot;,
          &quot;latitude&quot;: &quot;not available in demo dataset&quot;,
          &quot;longitude&quot;: &quot;not available in demo dataset&quot;,
          &quot;networkLocation&quot;: &quot;not available in demo dataset&quot;
      },
      &quot;hits&quot;: [
          {
              &quot;hitNumber&quot;: &quot;1&quot;,
              &quot;time&quot;: &quot;0&quot;,
              &quot;hour&quot;: &quot;23&quot;,
              &quot;minute&quot;: &quot;0&quot;,
              &quot;isInteraction&quot;: true,
              &quot;isEntrance&quot;: true,
              &quot;referer&quot;: &quot;https://www.youtube.com/yt/about/&quot;,
              &quot;page&quot;: {
                  &quot;pagePath&quot;: &quot;/home&quot;,
                  &quot;hostname&quot;: &quot;shop.googlemerchandisestore.com&quot;,
                  &quot;pageTitle&quot;: &quot;Home&quot;,
                  &quot;pagePathLevel1&quot;: &quot;/home&quot;,
                  &quot;pagePathLevel2&quot;: &quot;&quot;,
                  &quot;pagePathLevel3&quot;: &quot;&quot;,
                  &quot;pagePathLevel4&quot;: &quot;&quot;
              }
           }
         ]
  }
  </code></pre>
  <p>I am trying to extract key value pairs and create another json file with the same format. To achieve this objective here is what I tried to <strong>code</strong>:</p>
  <pre><code>     import json
  from datetime import datetime
  from datetime import timedelta
  
  visits_dict = {}
  hits_dict = {}
  
  hits_array = []
  
  with open(r&quot;C:\Users\rparpani\Documents\Test.json&quot;) as f:
      for line in f:
          json_object = json.loads(line)
  
          #This code builds the visit dictionary
          visits_dict[&quot;full_visitor_id&quot;] = json_object[&quot;fullVisitorId&quot;]
          visits_dict[&quot;visit_id&quot;] = json_object[&quot;visitId&quot;]
          visits_dict[&quot;visit_number&quot;] = json_object[&quot;visitNumber&quot;]
  
          visit_StartTime = datetime.fromtimestamp(int(json_object[&quot;visitStartTime&quot;])).isoformat()
          visits_dict[&quot;visit_start_time&quot;] = visit_StartTime
  
          visits_dict[&quot;browser&quot;] = json_object[&quot;device&quot;].get('browser')
          visits_dict[&quot;country&quot;] = json_object[&quot;geoNetwork&quot;].get('country')
  
          print(visits_dict)
  
          visitstartTime_unixValue = datetime.fromtimestamp(int(json_object[&quot;visitStartTime&quot;]))
          #print(visitstartTime_unixValue)
  
          #This code outputs the data to a visits.json file
          analyticsInfo_visitsOutput = open(&quot;C:\\Users\\rparpani\\Documents\\visits.json&quot;, &quot;a&quot;)
          json.dump(visits_dict, analyticsInfo_visitsOutput)
          analyticsInfo_visitsOutput.write('\n')
  
          # hits_arraycount = int(json_object.get('totals').get('hits'))
          # print(hits_arraycount)
  
          #This code builds the hits dictionary
          for j in json_object[&quot;hits&quot;]:
              #print(j.get('hitNumber'))
              hits_dict[&quot;hit_number&quot;] = j.get('hitNumber')
              hits_dict[&quot;hit_type&quot;] = j.get('type')
  
              hit_timeValue = int(j.get('time'))
              time_ms = timedelta(milliseconds=hit_timeValue)
              new_time = visitstartTime_unixValue + time_ms
              hits_dict[&quot;hit_timestamp&quot;] = new_time.isoformat()
  
              hits_dict[&quot;page_path&quot;] = j.get('page').get('pagePath')
              hits_dict[&quot;page_title&quot;] = j.get('page').get('pageTitle')
              hits_dict[&quot;hostname&quot;] = j.get('page').get('hostname')   
              
              print(hits_dict)
              
  
          
          #This code outputs the data to a visits.json file
              analyticsInfo_hitsOutput = open(&quot;C:\\Users\\rparpani\\Documents\\hits.json&quot;, &quot;a&quot;)
              json.dump(hits_dict, analyticsInfo_hitsOutput)
              analyticsInfo_hitsOutput.write('\n')
  
   
  </code></pre>
  <p>The code gives me the output the way I expect with the correct key value pairs but I'm a bit lost about how to store the hit values in a list like the original input.</p>
  <p>Can someone please give me suggestions on how to solve for this?</p>
  <p><strong>Actual Output:</strong></p>
  <pre><code>    {'full_visitor_id': '8159312408158297118', 'visit_id': '1470083489', 'visit_number': '151', 'visit_start_time': '2016-08-01T15:31:29', 'browser': 'Chrome', 'country': 'United States'}
  {'hit_number': '1', 'hit_type': 'PAGE', 'hit_timestamp': '2016-08-01T15:31:29', 'page_path': '/home', 'page_title': 'Home', 'hostname': 'shop.googlemerchandisestore.com'}
  {'hit_number': '2', 'hit_type': 'PAGE', 'hit_timestamp': '2016-08-01T15:36:18.206000', 'page_path': '/google+redesign/apparel/men++s/men++s+t+shirts', 'page_title': &quot;Men's-T-Shirts&quot;, 'hostname': 'shop.googlemerchandisestore.com'}
  {'hit_number': '3', 'hit_type': 'PAGE', 'hit_timestamp': '2016-08-01T15:40:17.730000', 'page_path': '/google+redesign/office', 'page_title': 'Office', 'hostname': 'shop.googlemerchandisestore.com'}
  </code></pre>
  <p>Expected Output:</p>
  <pre><code>{'full_visitor_id': '8159312408158297118', 'visit_id': '1470083489', 'visit_number': '151', 'visit_start_time': '2016-08-01T15:31:29', 'browser': 'Chrome', 'country': 'United States'}
  {'hits':[{'hit_number': '1', 'hit_type': 'PAGE', 'hit_timestamp': '2016-08-01T15:31:29', 'page_path': '/home', 'page_title': 'Home', 'hostname': 'shop.googlemerchandisestore.com'}
  {'hit_number': '2', 'hit_type': 'PAGE', 'hit_timestamp': '2016-08-01T15:36:18.206000', 'page_path': '/google+redesign/apparel/men++s/men++s+t+shirts', 'page_title': &quot;Men's-T-Shirts&quot;, 'hostname': 'shop.googlemerchandisestore.com'}
  {'hit_number': '3', 'hit_type': 'PAGE', 'hit_timestamp': '2016-08-01T15:40:17.730000', 'page_path': '/google+redesign/office', 'page_title': 'Office', 'hostname': 'shop.googlemerchandisestore.com'}]}
  </code></pre>