Save JavaScript object in a JSON file, grunt task

2

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]));
    }
  });
    
asked by GunBlade 10.08.2016 в 22:01
source

1 answer

1

First record a task that loads the json output and transforms it into the format you want. Modify this task to do exactly what you're looking for

grunt.registerTask('transformar', function() {
    var options = this.options({ entrada: 'entrada.json', salida: 'salida.json');
    // cargar el json
    var arreglo = grunt.file.readJSON(options.entrada);

    // ejemplo. cambia aqui la transformacion que necesitas
    var salida = { };
    salida[arreglo[0].text] = arreglo[0].es;

    // guarda el nuevo json, podrias guardar mas de uno repitiendo la llamada 
    // con otro nombre de archivo
    grunt.file.write(options.salida, JSON.stringify(salida));
});

Then configure both tasks, be sure to use the same input file name for this task and exit in the other.

grunt.initConfig({
  gss_to_json: {
    dist: {
      options: {
        // aqui lo que ya tienes
      },
      dest: 'salida_gss.json'
    }
  },
  transformar: { 
    options: {
      entrada: 'salida_gss.json',
      salida: 'transformada.json'
    }
  }
});

Finally, you execute them in sequence

grunt.registerTask('default', ['gss_to_json', 'transformar']);

I have not tried it but should not need big changes, let me know if you have any problems.

    
answered by 10.08.2016 / 22:09
source