阅读一堆JSON文件,对其进行转换并保存 [英] Read a bunch of JSON files, transform them, and save them

查看:167
本文介绍了阅读一堆JSON文件,对其进行转换并保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用 Gulp 来达到这个目的。


  1. .json 包含子目录的给定目录中的文件。
  2. 以某种方式转换它们,例如添加新的根级别等。


  3. 将它们保存到保存原始结构的新目录中。丢失是如何管读/写JSON到 src



    现在我有以下框架。

      gulp.task(migratefiles,function(){
    return gulp.src(files / ** / *。 json)
    .pipe(/ * WHAT HERE?* /)
    .pipe(gulp.dest(processed));
    });


    解决方案

    有很多方法可以做到这一点:



    (1)使用 gulp-json-transform 插件:

      var jsonTransform = require('gulp-json-transform'); 
    $ b gulp.task(migratefiles,function(){
    return gulp.src(files / ** / *。json)
    .pipe(jsonTransform(function (json,file){
    var transformedJson = {
    newRootLevel:json
    };
    return transformedJson;
    }))
    .pipe( gulp.dest(processed));
    });

    优点:


    • 易用

    • 支持异步处理(如果您返回 Promise

    • 允许访问每个 file



    缺点:$ b​​
    $ b (2)使用 https://www.npmjs.com/package/gulp-json-editorrel =nofollow> gulp-json-editor 插件: strong>

      var jeditor = require('gulp-json-editor'); 
    $ b gulp.task(migratefiles,function(){
    return gulp.src(files / ** / *。json)
    .pipe(jeditor(function (json){
    var transformedJson = {
    newRootLevel:json
    };
    return transformedJson;
    }))
    .pipe(gulp。 dest(processed));
    });

    优点:


    • 易用

    • 自动识别输入文件使用的缩进(两个空格,四个空格,制表符等)并相应地设置输出文件的格式。

    • 支援各种 js-beautify


      $缺点:



        li>似乎并不支持异步处理
      • 似乎没有办法访问每个文件的路径



      (3)手动(通过直接访问 vinyl 文件对象使用 地图-stream ):

        var map = require('map -流'); 
      $ b gulp.task(migratefiles,function(){
      return gulp.src(files / ** / *。json)
      .pipe(map(function (file,done){
      var json = JSON.parse(file.contents.toString());
      var transformedJson = {
      newRootLevel:json
      };
      file.contents = new Buffer(JSON.stringify(transformedJson));
      done(null,file);
      }))
      .pipe(gulp.dest(processed ));
      });

      优点:


      • 完全控制/访问所有内容

      • 支持异步处理(通过 done 回调)



      缺点:


      • 难以使用


      I'm trying to achieve this with Gulp.

      1. Read every .json file in a given directory including subdirectories.
      2. Transform them in some way, for example add a new root level, etc.
      3. Save them into a new directory keeping original structure.

      The point where I'm lost is how to pipe reading/writing JSON to src.

      I have the following skeleton now.

      gulp.task("migratefiles", function () {
        return gulp.src("files/**/*.json")
            .pipe(/* WHAT HERE? */)
            .pipe(gulp.dest("processed"));
      });
      

      解决方案

      There's a number of way you can do this:

      (1) Use the gulp-json-transform plugin:

      var jsonTransform = require('gulp-json-transform');
      
      gulp.task("migratefiles", function () {
        return gulp.src("files/**/*.json")
          .pipe(jsonTransform(function(json, file) {
            var transformedJson = {
              "newRootLevel": json
            };
            return transformedJson;
          }))
          .pipe(gulp.dest("processed"));
       });
      

      Pros:

      • Easy to use
      • Supports asynchronous processing (if you return a Promise)
      • Gives access to path of each file

      Cons:

      • Only rudimentary output formatting

      (2) Use the gulp-json-editor plugin:

      var jeditor = require('gulp-json-editor');
      
      gulp.task("migratefiles", function () {
         return gulp.src("files/**/*.json")
           .pipe(jeditor(function(json) {
             var transformedJson = {
               "newRootLevel": json
             };
             return transformedJson;
           }))
           .pipe(gulp.dest("processed"));
      });
      

      Pros:

      • Easy to use
      • Automatically recognizes the indentation your input files use (two spaces, four spaces, tabs etc.) and formats your output files accordingly
      • Supports various js-beautify options

      Cons:

      • Doesn't seem to support asynchronous processing
      • Doesn't seem to have a way to access path of each file

      (3) Do it manually (by directly accessing the vinyl file object using map-stream):

      var map = require('map-stream');
      
      gulp.task("migratefiles", function () {
         return gulp.src("files/**/*.json")
           .pipe(map(function(file, done) {
             var json = JSON.parse(file.contents.toString());
             var transformedJson = {
               "newRootLevel": json
             };
             file.contents = new Buffer(JSON.stringify(transformedJson));
             done(null, file);
           }))
           .pipe(gulp.dest("processed"));
      });
      

      Pros:

      • Full control/access over everything
      • Supports asynchronous processing (through a done callback)

      Cons:

      • Harder to use

      这篇关于阅读一堆JSON文件,对其进行转换并保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆