0

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.

kevinfromspace
  • 153
  • 1
  • 15
  • Possible duplicate of [write/add data in JSON file using node.js](https://stackoverflow.com/questions/36856232/write-add-data-in-json-file-using-node-js) – ponury-kostek May 22 '19 at 08:45
  • JSON files are difficult to append to (the whole file should be read, then parsed, then rewritten). I suggest using [JSONL](http://jsonlines.org/) instead - a format almost as easy to use as JSON. – Amadan May 22 '19 at 08:49

2 Answers2

2

Your statement:

var myArray1 = new Array ({
  Ex1 : "Ex1",
  Ex2 : "Ex2",
  Ex3 : "Ex3"
});

Creates an array of one object, and you do this twice, so you get two arrays, containing 1 object each.

You must create the first array, then keep pushing to it.

var myArray1 = new Array({
  Ex1 : "Ex1",
  Ex2 : "Ex2",
  Ex3 : "Ex3"
});

myArray1.push({
  Ex4: "Ex4",
  Ex5: "Ex5",
  Ex6: "Ex6"
})

console.log(myArray1)
Anurag Srivastava
  • 14,077
  • 4
  • 33
  • 43
1

Allright, from your code it should look like this:

const myArray1 = new Array ({
    Ex1 : "Ex1",
    Ex2 : "Ex2",
    Ex3 : "Ex3"
});

const myArray2 = new Array ({
    Ex4 : "Ex4",
    Ex5 : "Ex5",
    Ex6 : "Ex6"
});

const mergedArray = myArray1.concat(myArray2);
// Merged array is now:
// 0:
//  Ex1: "Ex1"
//  Ex2: "Ex2"
//  Ex3: "Ex3"
//  __proto__: Object
// 1:
//  Ex4: "Ex4"
//  Ex5: "Ex5"
//  Ex6: "Ex6"

JSON.stringify(mergedArray, null, 4);

// Returns formatted JSON.
// "[
//     {
//         "Ex1": "Ex1",
//         "Ex2": "Ex2",
//         "Ex3": "Ex3"
//     },
//     {
//         "Ex4": "Ex4",
//         "Ex5": "Ex5",
//         "Ex6": "Ex6"
//     }
// ]"

I tried it in Chrome console - it returns valid JSON. Check what might be different from what you are doing.

EternalLight
  • 1,323
  • 1
  • 11
  • 19