如何正确使用json-2-csv节点模块?

时间:2019-06-13 16:12:26

标签: javascript node.js json csv

我对nodeJS还是很陌生,所以也许我做错了什么,但是我正在尝试使用节点模块json-2-csv。但是,当我遵循文档并尝试使用它时,结果却未定义。

这是我尝试使用测试数据的代码。另外,我已经运行npm install json-2-csv并检查了package.json以查看其是否存在(版本3.5.4)

let converter = require('json-2-csv');


var array = [
    {
        "configurationId": {
        "id": "4000",
        "displayName": "4000",
        "uri": "/configuration/users/4000"
        },
        "actualValue": [
            {
            "id": "Agent",
            "displayName": "Agent",
            "uri": "/configuration/roles/Agent"
            },
            {
            "id": "SMS",
            "displayName": "SMS",
            "uri": "/configuration/roles/SMS"
            }
        ],
        "inheritedValue": [],
        "effectiveValue": [
            {
            "id": "Agent",
            "displayName": "Agent",
            "uri": "/configuration/roles/Agent"
            },
            {
            "id": "SMS",
            "displayName": "SMS",
            "uri": "/configuration/roles/SMS"
            }
        ]
    },
    {
        "configurationId": {
        "id": "4001",
        "displayName": "4001",
        "uri": "/configuration/users/4001"
        },
        "actualValue": [
            {
            "id": "Agent",
            "displayName": "Agent",
            "uri": "/configuration/roles/Agent"
            },
            {
            "id": "SMS",
            "displayName": "SMS",
            "uri": "/configuration/roles/SMS"
            }
        ],
        "inheritedValue": [],
        "effectiveValue": [
            {
            "id": "Agent",
            "displayName": "Agent",
            "uri": "/configuration/roles/Agent"
            },
            {
            "id": "SMS",
            "displayName": "SMS",
            "uri": "/configuration/roles/SMS"
            }
        ]
    },
    {
        "configurationId": {
        "id": "4002",
        "displayName": "40002",
        "uri": "/configuration/users/4002"
        },
        "actualValue": [
            {
            "id": "Agent",
            "displayName": "Agent",
            "uri": "/configuration/roles/Agent"
            },
            {
            "id": "SMS",
            "displayName": "SMS",
            "uri": "/configuration/roles/SMS"
            }
        ],
        "inheritedValue": [],
        "effectiveValue": [
            {
            "id": "Agent",
            "displayName": "Agent",
            "uri": "/configuration/roles/Agent"
            },
            {
            "id": "SMS",
            "displayName": "SMS",
            "uri": "/configuration/roles/SMS"
            }
        ]
    }
    ];

var result = converter.json2csv(array, function(err, csv){
    console.log("Error: ", err);
}, expandArrayObjects = true);
console.log("result is...", result);

重要的一点可能是此行:

var result = converter.json2csv(array, function(err, csv){
    console.log("Error: ", err);
}, expandArrayObjects = true);

这是我的输出:

Info: Start process (12:03:39 p.m.)
the result is... undefined
Error:  null
Info: End process (12:03:39 p.m.)

我希望输出是csv格式的文本,但是从上面可以看到,我不确定。我认为我使用的方式不正确,但实际上没有经验来理解我的错误。

1 个答案:

答案 0 :(得分:1)

open()的参数为.json2csv。根据[npm来源]

(csv, function, options)

因此,代码将是:

options - (Optional) A JSON document specifying any of the following key value pairs:

如果要使用其他设置,则var result = converter.json2csv(array, function(err, csv){ console.log("Error: ", err); console.log("Result: ", csv); }, {expandArrayObjects:true}); 对象将是:

options

但是我不确定这是否会奏效不是因为语法,因为我怀疑json2csv能否将深层json转换为CSV。

此外,{ expandArrayObjects: true, .... .... } 将没有数据,因为数据是通过回调传递的,并且它是一个异步函数。您需要result或在回调中使用变量。