I'm pretty new to this tool, so I'm trying to use everything I can in tasks. At the moment I'm using a plugin to read documents in Google Drive , the problem is not reading, it's the writing of the jsons. It is necessary to change the way the JSON is saved because it is not compatible with the way I have to read it.
Right now the output is: all.json
[
{
"text": "ACCEPT",
"es": "Aceptar",
"en": "Agreed"
}
]
and what I need is
en.json
{
"ACCEPT": "Aceptar"
}
en.json
{
"ACCEPT": "Agreed"
}
I'm thinking about reading all.json and creating the other files based on it ... I already have the objects created s but I do not know how to write the objects in .json files and en.json
Edit
The transformRow
that I am using is the same as that found in the example.
transformRow: function (row, header) {
var rowdata = {};
Object.keys(row).forEach(function (col) {
var key = header[col] ? header[col].toLowerCase().replace(/[^a-z]/g, "") : col;
rowdata[key] = row[col];
});
return rowdata;
}
The detail is that by checking the source code of the plugin, when you use the tranformRow function, it returns an array, which is not useful for the reading I need to perform.
Lines 61-70 source file gss_to_json.js
of the grunt_gss_to_json plugin.
if (options.transformRow) {
rows = Object
.keys(rows)
.map(function(row) {
return options.transformRow(rows[row], header);
})
.filter(function(row) {
return row !== false;
});
}
Result
Thanks to @md, the function is ready. I was really confused and now I like gruntjs a lot more to create complicated tasks.
grunt.registerMultiTask('tranformar_por_idioma', 'Crea un archivo por idioma segun el archivo base', function () {
var options = this.options({
debug: true
});
// cargar el json
var mapping = grunt.file.readJSON(options.input);
// ejemplo. cambia aqui la transformacion que necesitas
var salida = {};
for (var arrayelement in mapping) {
var key = null;
for (var keyobject in mapping[arrayelement]) {
if (keyobject.toUpperCase() === 'TEXT') {
key = mapping[arrayelement][keyobject];
} else {
if (typeof salida[keyobject.toLowerCase()] === 'undefined') {
salida[keyobject.toLowerCase()] = {};
}
salida[keyobject.toLowerCase()][key] = mapping[arrayelement][keyobject];
}
}
}
if (options.debug) {
console.log(salida);
}
// guarda el nuevo json
for (var key in salida) {
grunt.file.write(options.outpath + "/" + key + ".json", JSON.stringify(salida[key]));
}
});