I'm trying to save data to a JSON file, the first save goes fine, the second save breaks the JSON. Here is what my array looks like (simplified for example):
var myArray1 = new Array ({
Ex1 : "Ex1",
Ex2 : "Ex2",
Ex3 : "Ex3"
});
I save this to JSON, which returns valid json, I read it and try to save another array like this on top of this one:
var myArray2 = new Array ({
Ex4 : "Ex4",
Ex5 : "Ex5",
Ex6 : "Ex6"
});
I've tried using concat which creates this in Chrome Console:
0:
Ex1: "Ex1"
Ex2: "Ex2"
Ex3: "Ex3"
__proto__: Object
1:
Ex4: "Ex4"
Ex5: "Ex5"
Ex6: "Ex6"
__proto__: Object
length: 2
__proto__: Array(0)
I would assume using JSON.stringify now would make it valid JSON but when I save it to JSON it gives me this:
[{
"chosenDate": "May_2019_1",
"shiftName": "Test 1",
"startTime": "00:00",
"endTime": "01:00",
"payPerHour": "1.00"
}]
[{
"chosenDate": "May_2019_16",
"shiftName": "Test 2",
"startTime": "01:00",
"endTime": "02:00",
"payPerHour": "2.00"
}]
The first JSON was the exact same up until the first "]" so concat did nothing in the end? How can I solve this so it makes valid JSON?
I don't know if it changes anything but I'm trying to make an ionicv1 app, using the cordova-file-plugin to save JSON.
EDIT: So I'm unsure why but when trying to use my example in Chrome it works, but when running it through my app, actually putting it in a file and reading it from the file it returns the broken JSON. The process is:
- create first array.
- stringify it and write to json file.
- read file and parse json.
- create second array.
- concat the arrays.
- stringify and write to json.
and the file has the broken JSON. I guess somewhere in writing it or reading something is not going quite right, also setting it apart from the possible duplicate post.