Nodejs file write and export operations – get the local JSON file and export the specified format JSON


Export and write jnos files:
Through the FS operation file built in nodejs, get the local JSON data, traverse and format to generate new JSON data, and export it to the local

var fs = require('fs'); // File module
var path = require('path'); // System path module

//Get local JSON file
var file = path.join(__dirname, 'src/path.json');
var allArr=[];
//Read JSON file
fs.readFile(file, 'utf-8', function(err, data) {
if (err) {
    Res.send ('File reading failed ');
} else {
    var newData=JSON.parse(data);
    //Format data
    for(let j=0;j<newData.length;j++){
        let list=[];
        var index=j*2;

    var content = JSON.stringify(allArr); 

    //Specify the creation directory and file name__ Dirname is the directory where the current JS file is executed
    var file = path.join(__dirname, 'json/path.json'); 

    //Write file
    fs.writeFile(file, content, function(err) {
        if (err) {
            return console.log(err);
        Console.log ('File created successfully, address: '+ file);

Path.json data:



Generate a new JSON file: