Jérôme Jérôme - 1 month ago 5
Javascript Question

Made a JSON file with multiple objects with a simple template

I have a template JSON file used by REST API POST request. This file contains this object

{
"Subject": "template",
"Body": {
"ContentType": "HTML",
"Content": "template"
},
"Start": {
"DateTime": "2014-02-02T18:00:00",
"TimeZone": "Europe/Paris"
},
"End": {
"DateTime": "2014-02-02T19:00:00",
"TimeZone": "Europe/Paris"
}
}


I wrote a little function to change some datas inside this object and save this into another json file called
jsonOutput.json


There is the function to change datas, this is working well.

function insertDatasJson (res) {
let fs = require('fs');
let base = require('../public/json/template.json');

base.Subject = 'f';
base.Body.Content = 'e';
base.Start.DateTime = '2016-11-13T08:30:00';
base.End.DateTime = '2016-11-13T17:30:00';

fs.writeFileSync('./public/json/output/jsonOutput.json', JSON.stringify(base, null, 4));
}


Now I trying to do the same modifications but I have a lot of datas to modify (multiple Subject, multiple Content and multiple Start/End dates)

I trying to have an output file like this

[
{
"Subject": "1",
"Body": {
"ContentType": "HTML",
"Content": "1"
},
"Start": {
"DateTime": "2014-02-02T18:00:00",
"TimeZone": "Europe/Paris"
},
"End": {
"DateTime": "2014-02-02T19:00:00",
"TimeZone": "Europe/Paris"
}
},
// some other objects ...
{
"Subject": "n",
"Body": {
"ContentType": "HTML",
"Content": "n"
},
"Start": {
"DateTime": "2014-02-02T18:00:00",
"TimeZone": "Europe/Paris"
},
"End": {
"DateTime": "2014-02-02T19:00:00",
"TimeZone": "Europe/Paris"
}
}
]


The problem is, if I do a simple loop in my little function, every data will overwrite the one before and I will get an output file like the template.

How can I make to not overwrite datas during the loop and to have an output file like my example ?

Answer

if you are writing all data at once, then you need to do create an array, push all objects to array and write the array to file

function insertDatasJson (res) {
    let fs = require('fs');
    let base = require('../public/json/template.json');
    let result = [];
    for (/*you loop statmeent*/) {
      let obj = JSON.parse(JSON.stringify(base));  // or your preferred way of deep copying
      obj.Subject = 'f';
      obj.Body.Content = 'e';
      obj.Start.DateTime = '2016-11-13T08:30:00';
      obj.End.DateTime = '2016-11-13T17:30:00';
      result.push(obj);
    }

    fs.writeFileSync('./public/json/output/jsonOutput.json', JSON.stringify(result, null, 4));
}

Or if you want to write data in multiple runs, then

function insertDatasJson (res) {
    let fs = require('fs');
    let base = require('../public/json/template.json');
    let data = require('./public/json/output/jsonOutput.json');
    base.Subject = 'f';
    base.Body.Content = 'e';
    base.Start.DateTime = '2016-11-13T08:30:00';
    base.End.DateTime = '2016-11-13T17:30:00';
    data.push(base);
    fs.writeFileSync('./public/json/output/jsonOutput.json', JSON.stringify(data, null, 4));
}

However, in second case, you need to add some code to handle the case of first run when there is no existing data in the output file, or file doesn't exist. Another way to handle that condition would be to initialize the output file with empty JSON array

[]

EDIT: In both cases, appending to the existing file will not work as it will generate invalid JSON.

Comments