title: Python json file generator add `][` between every 1000 elements
body: <p>Below I have a function I've created that randomly generates <code>n</code> number of json objects. It then groups them in groups of 1000 and writes them to a file. The issue I've run into is that each time it writes a group of 1000 it add <code>][</code> between them making the json invalid. I'm fairly new to python and have tried looping over each element and doing a <code>.replace('][', ',')</code> but that isn't working. Any and all help would be greatly appreciated. </p>
  
  <pre><code>import random
  import json as json
  import sys
  import gzip
  import common.common_functions as common
  
  def get_args(p_args):
      if "--num-records" in p_args:
          num_records = int(p_args[int(p_args.index('--num-records'))+1])
      else:
          print("number of records defaulting to 1000")
          num_records = 1000
  
      if "--num-groups" in p_args:
          num_groups = int(p_args[int(p_args.index('--num-groups'))+1])
      else:
          print("number of groups defaulting to 10")
          num_groups = 10
  
      if "--compress" in p_args:
          compression = True
      else:
          compression = False
  
      if "--file-name" in p_args:
          file_name = p_args[int(p_args.index('--file-name'))+1]
      else:
          print("file_name param must be submitted")
          exit(-1)
  
      return num_records, num_groups, file_name, compression
  
  
  def main():
  
      num_records, num_groups, file_name, compression = get_args(sys.argv)
  
      if compression:
          write_mode = 'wt'
      else:
          write_mode = 'w'
  
      state, min_zip, max_zip = common.pick_state()
  
      groups = common.generate_groups(num_groups)
  
      fnames, lnames, mnames, dobs, zips, genders,  group_ids, coverages = [], [], [], [], [], [], [], []
  
      for record_num in (num + 1 for num in range(num_records)):
  
          fnames.append(common.pick_fname())
          mnames.append(common.gen_mi())
          lnames.append(common.pick_lname())
          dobs.append(common.gen_date_in_range("1925/01/01","2000/12/31","%Y/%m/%d","%Y/%m/%d"))
          zips.append(common.gen_zip(min_zip, max_zip))
          genders.append(common.gen_gender())
          group_ids.append(common.pick_group(groups))
          coverages.append(random.randint(1000,500000))
  
          sample_records = [{"fname": fname, "mname": mname, "lname": lname, "dob:": dob, "zip": zip, "gender": gender, "group_id": group_id, "coverage": coverage} \
                            for fname, mname, lname, dob, zip, gender, group_id, coverage in zip(fnames,mnames,lnames,dobs,zips,genders,group_ids,coverages)]
  
          if (record_num % 1000 == 0):
              if compression:
                  with gzip.open(file_name, write_mode, encoding='ascii') as zipfile:
                      json.dump(sample_records, zipfile)
                  if write_mode == 'wt':
                      write_mod = 'at'
              else:
                  with open(file_name, write_mode) as json_file:
                      json.dump(sample_records, json_file)
                  if write_mode == 'w':
                      write_mode = 'a'
  
              fnames, lnames, mnames, dobs, zips, genders,  group_ids, coverages = [], [], [], [], [], [], [], []
  
              print (record_num)
  
      if compression:
          with gzip.open(file_name, write_mode, encoding='ascii') as zipfile:
              json.dump(sample_records, zipfile)
      else:
           with open(file_name, write_mode) as json_file:
              json.dump(sample_records, json_file)
  
  
  main()
  </code></pre>
  
  <p>Here is what the output looks like: </p>
  
  <pre><code>{
      "fname": "JAMES",
      "mname": "W",
      "lname": "KIRVIN",
      "dob:": "1937/04/25",
      "zip": "43650",
      "gender": "F",
      "group_id": "p28m80z8",
      "coverage": 264200
  }, {
      "fname": "JUSTIN",
      "mname": "P",
      "lname": "REUTEBUCH",
      "dob:": "1941/02/09",
      "zip": "44254",
      "gender": "M",
      "group_id": "6x6olrgc",
      "coverage": 251417
  }][{ &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; Here is the issue
      "fname": "EMILY",
      "mname": "U",
      "lname": "STEEDLE",
      "dob:": "1948/12/30",
      "zip": "44524",
      "gender": "M",
      "group_id": "6x7d4c43",
      "coverage": 25278
  }, {
  </code></pre>
  
  <p>Ideally I would like the out put json that is saved to be valid</p>