Saving with JSON for the first time

Running into a problem which I’m sure has a simple fix when I use the rob miracle loadsave function.  See code:

json = require( “json” )  – Include the Corona JSON library

local loadsave = require( “loadsave” )

myData = loadsave.loadTable(“myData.json”)

myData={}

myData.someValue = true

loadsave.saveTable(myData, “myData.json”)

if I run this code at the beginning of running my app - it works but - every time I launch it I’m immediately saving over my old data with a clear slate (making saving pointless), whereas this code:

json = require( “json” )  – Include the Corona JSON library

local loadsave = require( “loadsave” )

myData = loadsave.loadTable(“myData.json”)

print(myData.someValue)  – this brings up an error saying that it can’t because myData is a nil value

will bring up an error reading myData as null, *if* it’s the first time launching an app - as nothing has ever been saved yet. 

Just curious as to what the workaround would be. Regards.

json = require( "json" )  -- Include the Corona JSON library local loadsave = require( "loadsave" ) myData = loadsave.loadTable("myData.json") if (myData == nil) then      myData={}      myData.someValue = true      loadsave.saveTable(myData, "myData.json") end

I’m pretty sure this is what you want? Just use a simple if statement to detect whether the file is already in place. If I misunderstood your question, please let me know.

Worked great, thanks!

json = require( "json" )  -- Include the Corona JSON library local loadsave = require( "loadsave" ) myData = loadsave.loadTable("myData.json") if (myData == nil) then      myData={}      myData.someValue = true      loadsave.saveTable(myData, "myData.json") end

I’m pretty sure this is what you want? Just use a simple if statement to detect whether the file is already in place. If I misunderstood your question, please let me know.

Worked great, thanks!