Grunt 读取 json 文件并将字段推送到另一个 json 文件

Grunt Read json file and push field into another json file

我正在尝试从 bower.json 中读取数据并将其推送到我的项目的现有 json 文件中

var mapping = grunt.file.readJSON('bower.json');
var map = grunt.file.readJSON('other.json);

如何将它推入另一个文件?我已经尝试过 push() 和 write() 谢谢

如果您只想将一个 json 文件硬复制到另一个文件,您可以使用类似的东西:

  var mapping = grunt.file.readJSON('bower.json');//read in the current
  grunt.file.write("other.json", JSON.stringify(mapping, null, 2));

或者如果您想将 other.json 中的某个字段设置为与 bower.json 相同:

  var mapping = grunt.file.readJSON('bower.json');//read in bower
  var map = grunt.file.readJSON('other.json');//read in other
  map["version"] = mapping["version"]; //copy mapping version to map version
  grunt.file.write("other.json", JSON.stringify(map, null, 2)); //save other.json