使用功能文件更新JSON元素

时间:2018-03-28 13:18:56

标签: javascript json node.js rest parsing

我想更新JSON文件中某个属性的值。如果我在代码中传递属性名称,我可以更新它。但是,如果我尝试在feature文件中传递该属性名称,则会在JSON文件中创建其他元素。

工作代码:

await (function replaceJSONData(callback) {
        fs.readFile(requestPath, 'utf8', function (err, data) {
            var tempJSON = JSON.parse(data);
            tempJSON. = customer.personalDetails.customerFullName = "Amanda"
            console.log(tempJSON);
        });
    });

在JSON请求中失败并创建新节点元素:

//NOTE: nodeValue_1 is argument coming from feature file which has value as
|nodeValue_1                              |
|customer.personalDetails.customerFullName|

    this.Given(/^I replace (.*) and (.*) for rest service (.*) from (.*)$/, function (nodeValue_1, nodeValue_2, rest_url, filePath, callback) {
    var requestPath = requestDataPath+'\\'+rest_url+'-req.json';
    var currentAccount = fs.readFileSync(runTimeDataPath+'\\'+filePath, 'utf8');
    console.log(nodeValue_1);
    await (function replaceJSONData(callback) {
        fs.readFile(requestPath, 'utf8', function (err, data) {
            var tempJSON = JSON.parse(data);
//This will add new element called as nodeValue_1 in JSON - FAILS
            tempJSON.nodeValue_1 = "Amanda";
            console.log(tempJSON);
        });
    });
});

JSON请求更新:

{
"customer": {
    "personalDetails": {
        "userTitle": "Mr",
        "customerFullName": "MrSchaumann",
        "dateOfBirth": "1980-05-08",
        "customerSureName": "Baganz"
    }
}
}

2 个答案:

答案 0 :(得分:0)

这里tempJSON.nodeValue_1不会工作,因为nodeValue_1是一个变量。尝试使用

tempJSON[nodeValue_1]

nodeValue_1不会以点表示法解析它的值,但是如果你使用tempJSON [nodeValue_1]它将得到解决。

所以你的最终代码如下所示:

//NOTE: nodeValue_1 is argument coming from feature file which has value as
|nodeValue_1                              |
|customer.personalDetails.customerFullName|

this.Given(/^I replace (.*) and (.*) for rest service (.*) from (.*)$/,
function (nodeValue_1, nodeValue_2, rest_url, filePath, callback) {
var requestPath = requestDataPath+'\\'+rest_url+'-req.json';
var currentAccount = fs.readFileSync(runTimeDataPath+'\\'+filePath, 'utf8');
console.log(nodeValue_1);
await (function replaceJSONData(callback) {
    fs.readFile(requestPath, 'utf8', function (err, data) {
        var tempJSON = JSON.parse(data);
        //This will update the value as you expect.
        tempJSON[nodeValue_1] = "Amanda";
        console.log(tempJSON);
    });
  });
}); 

答案 1 :(得分:0)

将您的路径(nodeValue_1)拆分为数组并按顺序应用它,删除路径中的lastNode并将其用于更新。

var p = nodeValue_1.split(".");
var lastNode = p.pop(); 
var resultNode = tempJSON;
p.map(function(itm){
    resultNode = resultNode[itm];    
});  
resultNode[lastNode] = "Amanda";
console.log(JSON.stringify(tempJSON));

最终你的代码应该像

this.Given(/^I replace (.*) and (.*) for rest service (.*) from (.*)$/, function (nodeValue_1, nodeValue_2, rest_url, filePath, callback) {
    var requestPath = requestDataPath+'\\'+rest_url+'-req.json';
    var currentAccount = fs.readFileSync(runTimeDataPath+'\\'+filePath, 'utf8');
    console.log(nodeValue_1);
    await (function replaceJSONData(callback) {
        fs.readFile(requestPath, 'utf8', function (err, data) {
           var tempJSON = JSON.parse(data);
           var p = nodeValue_1.split(".");
           var lastNode = p.pop(); 
           var resultNode = tempJSON;
           p.map(function(itm){
             resultNode = resultNode[itm];    
          });  
          resultNode[lastNode] = "Amanda";
            console.log(JSON.stringify(tempJSON));
        });
    });
});