title: Python writing to json file object by object
body: <p>I am looping through a bunch of urls to medium sized json files, that I'm trying to combine into a single file. Currently it's appending the json data for each file to a list, that I can then write to a single Json file</p>
  
  <pre><code>data = []
  
  for i in range(50):
      item = tree.getroot()[i][0]
      with urllib.request.urlopen(url + item.text) as f:
          for line in f:
              data.append(json.loads(line))
  
  # Save to file
  with open('data.json', 'w') as outfile:
       json.dump(data, outfile)
  </code></pre>
  
  <p>However, I'm not sure how scalable this method is and I will eventually have to combine 100s of files this way. If the data list becomes way to too large I'm thinking that trying to write it in one go would cause my system to crash due to memory issues? Is there a way to write continuously to a json file, so instead of appending inside the loop like this:</p>
  
  <pre><code>data.append(json.loads(line))
  </code></pre>
  
  <p>I would instead write to the file inside each loop with something like this:</p>
  
  <pre><code>with open('data.json', 'w') as outfile:
       json.writedata, outfile)
  </code></pre>
  
  <p>That way I'll be building the file as I go and can clear the memory between each loop?</p>