如何通过Node.js清理和编辑JSON文件

时间:2019-02-05 20:30:21

标签: node.js json

我有一个JSON文件,其中包含一些我需要删除的数据:

{
    "Count": 2, 
    "Items": [
        {
            "character": {
                "S": "1234"
            }, 
            "location": {
                "S": "east"
            }, 
            "inputs": {
                "S": "1"
            }, 
            "region": {
                "S": "north"
            }, 
            "access": {
                "S": "0"
            }  "S": "physical"
        }
    ], 
    "ScannedCount": 2, 
    "ConsumedCapacity": null
}

我要删除的是项目[]括号之外的所有内容

1 个答案:

答案 0 :(得分:0)

我发现这行得通。谢谢@ Zero298的初衷。

var fs = require('fs');
var newJSON = '';
newJSON = JSON.stringify(JSON.parse(fs.readFileSync('my_file.json', 'utf8'))['Items'], null, 4);
console.log("THIS IS THE NEW JSON");
console.log(newJSON);