title: Saving lists to JSON on demand
body: <pre><code>def listsToFile(listName, listToSave):
      #Check if file exists : if the file does not exist, create it
      if(os.path.isfile('data.json') != True):
          dataFile = open(&quot;data.json&quot;, &quot;w&quot;)
          data = {}
          json.dump(data, dataFile)
          dataFile.close()
  
      with open('data.json', 'r+') as readFile: 
          readData = json.load(readFile)
          
      readData[listName] = listToSave
      with open('data.json', 'r+') as outputToFile: 
          data = json.load(outputToFile)
          data.update(readData)
          outputToFile.seek(0)
          json.dump(data, outputToFile)
  </code></pre>
  <p>I'm having trouble with saving multiple python lists to a JSON using the code above.
  When the code is run on a clean <code>data.json</code> file, or when the file hasn't yet been created, the code executes without issues.
  If I run the code again after I've already created <code>data.json</code> file and filled it with necessary data, the code occasionally fails with the following error:</p>
  <pre><code>raise JSONDecodeError(&quot;Extra data&quot;, s, end)
  json.decoder.JSONDecodeError: Extra data: line 1 column 2505 (char 2504)
  </code></pre>
  <p>Below is an example of a properly generated JSON file:</p>
  <pre><code>{&quot;values1&quot;: [0.0, 0.9510565162951535, 0.5877852522924732, -0.587785252292473, -0.9510565162951536, 0.0, 0.9510565162951535, 0.5877852522924732, -0.587785252292473, -0.9510565162951536], &quot;values2&quot;: [-0.005169993459294982, 0.900960478450234, 0.688082399145687, -0.6991548418279838, -0.7067674655652273, 0.02282072685197254, 1.0032296059350259, 0.264000562571027, -0.7222227190160149, -1.4026325206242891], &quot;values3&quot;: [19.042863922620974, 37.87458461655387, 33.45043085989223, 4.620074671576601, 4.461864779721113, 19.62458304946258, 40.0, 24.636920062805316, 4.140664880521349, -10.0], &quot;values4&quot;: [19.042863922620974, 37.37458461655387, 32.45043085989223, 3.1200746715766012, 2.461864779721113, 17.12458304946258, 37.0, 21.136920062805316, 0.1406648805213493, -14.5], &quot;values5&quot;: [19.042863922620974, 37.37458461655387, 32.45043085989223, 3.1200746715766012, 84.0204012020356, 17.12458304946258, 37.0, 3.475329107497103, 75.8901259294412, 45.46783947206504], &quot;values6&quot;: [19.042863922620974, 37.37458461655387, 32.45043085989223, 25.120074671576603, 106.0204012020356, 39.12458304946258, 59.0, 25.475329107497103, 75.8901259294412, 45.46783947206504]}
  </code></pre>
  <p>Below is an example of what happens when I run the code and try to save different lists to the already existing and filled <code>data.json</code> file:</p>
  <pre><code>{&quot;values1&quot;: [0.0, 0.5877852522924731, 0.9510565162951535, 0.9510565162951536, 0.5877852522924732, 1.2246467991473532e-16, -0.587785252292473, -0.9510565162951535, -0.9510565162951536, -0.5877852522924734, 0.0, 0.5877852522924731, 0.9510565162951535, 0.9510565162951536, 0.5877852522924732, 1.2246467991473532e-16, -0.587785252292473, -0.9510565162951535, -0.9510565162951536, -0.5877852522924734], &quot;values2&quot;: [-0.10117028598579918, 0.7779790908824286, 1.200393390118824, 1.0838219894046404, 0.15757602921920028, -0.0067210289946661814, -0.5273871827688272, -1.1565635352862502, -0.8323326253928102, -1.2756839944935217, -0.05004380182838799, 0.6714864299550243, 1.0662910625661586, 0.7590821618652706, 1.0445118435048915, -0.06508749032370621, -0.40940979225502727, -0.6795514583892261, -0.8884786917027516, -0.6762588207898561], &quot;values3&quot;: [6.858612697312811, 15.735045460801214, 20.0, 18.823023449927092, 9.471074618060788, 7.812230479799041, 2.555264794781925, -3.797286587774387, -0.52365192768276, -5.0, 7.374817122860437, 14.659830065785634, 18.646020431489255, 15.544250444310677, 18.426124042177932, 7.222926792323841, 3.746437082519922, 1.0189206909381987, -1.090537141558372, 1.052165185030268], &quot;values4&quot;: [5.781634685737641, 13.351407441895873, 19.0, 15.543929768576376, 13.69489283405558, 10.464760506006371, 0.3539527971091214, -8.5, -3.8334192237799343, -1.8576091755881983, 8.59357984630877, 8.034254186327477, 9.959064845492588, 13.037705232284743, 12.395566807169725, 1.0706082945977524, -2.3644898895249664, -8.913833863581623, -4.831138451105485, -6.463793291134955], &quot;values5&quot;: [5.781634685737641, 13.351407441895873, 19.0, 15.543929768576376, 13.69489283405558, 30.782546604725596, 0.3539527971091214, -8.5, -3.8334192237799343, -1.8576091755881983, 38.0, 8.034254186327477, 9.959064845492588, 27.084472735684002, 12.395566807169725, 1.0706082945977524, -2.3644898895249664, -8.913833863581623, 2.9851725802192135, -6.463793291134955], &quot;values6&quot;: [5.781634685737641, 13.351407441895873, 19.0, 37.543929768576376, 35.69489283405558, 52.782546604725596, 22.353952797109123, 13.5, -3.8334192237799343, -1.8576091755881983, 38.0, 8.034254186327477, 9.959064845492588, 27.084472735684002, 12.395566807169725, 1.0706082945977524, -2.3644898895249664, -8.913833863581623, 2.9851725802192135, -6.463793291134955]}955]}
  </code></pre>
  <p>As you can see, at the very end of the second JSON file, there is a junk value <code>955</code> and <code>]}</code> has been added.</p>
  <p>The lists aren't added all at once. They are added one by one as each list is generated.
  I am using JSON because I can later access all of the lists by their name.</p>