nodejs的json to csv 转换

node中有json2csv模块,但是当json数据的key未事先指定,并且有嵌套结构的时候,嵌套在内层的数据无法被识别并转换。
参考一个在线的json to csv转换工具Convert JSON to CSV,及其源码parse_object 和 csvkit
先对json数据进行递归遍历,将其拍平,然后再利用json2csv模块。
代码如下:

var fs = require('fs');
var filename = 'data.txt';

function allItems(filename) {
    console.log("read file", filename)
    var contents = fs.readFileSync(filename).toString().split("\n")

    var arr = []

    contents.forEach(s => {
        try {
            arr.push(JSON.parse(s))
        } catch (e) {
            console.log("parse error", e)
            console.log("parse error", s)
        }
    })
    return arr
}

function parseObject(obj, path) {
    if (path == undefined)
        path = "";
    
    var type = obj.constructor;

    var scalar = (type == Number || type == String || type == Boolean || type == null);

    if (type == Array || type == Object) {
        var d = {};
        for (var i in obj) {

            var newD = parseObject(obj[i], path + i + ".");
            Object.assign(d, newD);
        }

        return d;
    }
    else if (scalar) {
        var d = {};
        var endPath = path.substr(0, path.length-1);
        d[endPath] = obj;
        return d;
    }
    else return {};
}

function csv() {
    var arr = allItems(filename);
    var arrNew = [];
    var json2csv = require('json2csv');
    for(var i in arr) {
        arrNew.push(parseObject(arr[i]));
    }

    try {
        var result = json2csv({ data: arrNew });
        fs.writeFile(filename+'.csv', result);

    } catch (err) {
        // Errors are thrown for bad options, or if the data is empty and no fields are provided.
        // Be sure to provide fields if it is possible that your data array will be empty.
        console.error("convert", err, err.stack);
    }
}

csv()

你可能感兴趣的:(nodejs的json to csv 转换)