重组JSON并输出到另一个文件

时间:2018-09-16 12:45:37

标签: javascript json node.js

我得到了很长的一级json数据列表,看起来像这样: json-old.json

[
{"stock": "abc", "volume": "45434", "price": "31", "date": "10/12/12"},
{"stock": "abc", "volume": "45435", "price": "30", "date": "10/13/12"},
{"stock": "xyz", "volume": "34465", "price": "14", "date": "10/12/12"},
{"stock": "xyz", "volume": "34434", "price": "14", "date": "10/13/12"},
{"stock": "zzz", "volume": "76755", "price": "65", "date": "10/12/12"},
{"stock": "zzz", "volume": "85646", "price": "67", "date": "10/13/12"}
]

如何获取上面的文件并将其输出到新文件: json-new.json

[
{ 
   "abc": {
      "10/12/12": { "volume": "45434", "price": "31" },
      "10/13/12": { "volume": "45435", "price": "30" }
   }
},
{
   "xyz": {
      "10/12/12": { "volume": "34465", "price": "14" },
      "10/13/12": { "volume": "34434", "price": "14" }
   }
},
{
   "zzz": {
      "10/12/12": { "volume": "76755", "price": "65" },
      "10/13/12": { "volume": "85646", "price": "67" }
   }
}
]

基本上,我想将数据组织和嵌套在“股票”属性下,然后将其余数据嵌套在“日期”属性下。

是否存在可用于在javascript或nodejs中自动执行此过程的库?我在文件夹中有很多这样的文件,并希望将其输出到具有所需输出的单个文件中(以便我可以上载到Firebase之类的数据库)。

3 个答案:

答案 0 :(得分:1)

您可以如下使用for循环。

var arr = [
        { "stock": "abc", "volume": "45434", "price": "31", "date": "10/12/12" },
        { "stock": "abc", "volume": "45435", "price": "30", "date": "10/13/12" },
        { "stock": "xyz", "volume": "34465", "price": "14", "date": "10/12/12" },
        { "stock": "xyz", "volume": "34434", "price": "14", "date": "10/13/12" },
        { "stock": "zzz", "volume": "76755", "price": "65", "date": "10/12/12" },
        { "stock": "zzz", "volume": "85646", "price": "67", "date": "10/13/12" }
    ];
    var group = [];
    var json = []
    for (var item in arr)
    {
        var itm=arr[item];
        var date = {
            'volume': itm.volume,
            'price': itm.price
        }
        var stock = group[itm.stock] || {};
        stock[itm.date] = date;
        group[itm.stock] = stock;
    }
    for (i in group) {
        var obj = {}
        obj[i] = group[i];
        json.push(obj)
    }
    console.log(json);
   // console.log(JSON.stringify(json));

并且您使用json作为新格式

答案 1 :(得分:1)

您可以使用Array.reduce

const s = [
    { "stock": "abc", "volume": "45434", "price": "31", "date": "10/12/12" },
    { "stock": "abc", "volume": "45435", "price": "30", "date": "10/13/12" },
    { "stock": "xyz", "volume": "34465", "price": "14", "date": "10/12/12" },
    { "stock": "xyz", "volume": "34434", "price": "14", "date": "10/13/12" },
    { "stock": "zzz", "volume": "76755", "price": "65", "date": "10/12/12" },
    { "stock": "zzz", "volume": "85646", "price": "67", "date": "10/13/12" }
]

const g = s.reduce((ret, o) => {
    const {stock, date, ...props} = o

    const items = ret[stock] || {}
    items[date] = props
    ret[stock] = items

    return ret
}, {})

console.log([g])

答案 2 :(得分:0)

这应该做到:

const fs = require('fs')

const groupBy = (items, key) => items.reduce(
    (result, item) => ({
      ...result,
      [item[key]]: [
        ...(result[item[key]] || []),
        item,
      ],
    }), 
    {},
);

const inFileName = process.argv[2]
const outFileName = process.argv[3]

const json = JSON.parse(fs.readFileSync(inFileName, 'utf8'));
const groupedStock = groupBy(json,'stock')
const groupedDate = {}
for(s in groupedStock)
    groupedDate[s] = groupBy(groupedStock[s],'date')

const filtered = {}
for(s in groupedDate){
    filtered[s] = {}
    for (d in groupedDate[s]) {
        filtered[s][d] = {
            price: groupedDate[s][d][0].price,
            volume: groupedDate[s][d][0].volume
        }
    }
}

const res = []
for(s in filtered)
    res.push({
        [s]: filtered[s]
    })


fs.writeFileSync(outFileName, JSON.stringify(res,null,2));

使用node convert.js json-old.json json-new.json

运行程序