简体   繁体   English

如何使用 NodeJS 将巨大的 Json 文件读入单个对象?

[英]How do I read a Huge Json file into a single object using NodeJS?

I'm upgrading a backend system that uses require('./file.json') to read a 1GB Json file into a object, and then passes that object to other parts of the system to be used.我正在升级一个后端系统,它使用require('./file.json')将 1GB Json 文件读入一个对象,然后将该对象传递给系统的其他部分以供使用。

I'm aware of two ways to read json files into an object我知道两种将 json 文件读入对象的方法

const fs = require('fs');
const rawdata = fs.readFileSync('file.json');
const data = JSON.parse(rawdata);

and

const data = require('./file.json');

This works fine in older versions of node(12) but not in newer version (14 or 16)这在旧版本的 node(12) 中工作正常,但在较新版本(14 或 16)中无效

So I need to find another way to get this 1GB big file.json into const data without running into the ERR_STRING_TOO_LONG / Cannot create a string longer than 0x1fffffe8 characters error.所以我需要找到另一种方法来将这个 1GB 大file.json转换为const data而不会ERR_STRING_TOO_LONG / Cannot create a string longer than 0x1fffffe8 characters错误。

I've seen examples on StackOverflow etc. on how to Stream Huge Json files like this and break it down into smaller objects processing them individually, but this is not what I'm looking for, I need it in one data object so that entire parts of the system that expect a single data object don't have to be refactored to handle a stream.我已经在 StackOverflow 等上看到了关于如何像这样流式传输巨大的 Json 文件并将其分解成更小的对象单独处理它们的示例,但这不是我要找的,我需要它在一个data对象中,以便整个不需要重构期望单个data对象的系统部分来处理流。

Note: The Top-level object in the Json file is not an array.注意:Json 文件中的 Top-level 对象不是数组。

Using big-json solves this problem.使用big-json解决了这个问题。

npm install big-json
const fs = require('fs');
const path = require('path');
const json = require('big-json');
 
const readStream = fs.createReadStream('file.json');
const parseStream = json.createParseStream();
 
parseStream.on('data', function(pojo) {
    // => receive reconstructed POJO
});
 
readStream.pipe(parseStream);

You need to stream it, so process it in chunks instead of loading it all into memory in a single point in time.您需要流式传输它,因此将其分块处理,而不是在单个时间点将其全部加载到内存中。


const fs = require("fs");
const stream = fs.createReadStream("file.json");

stream.on("data", (data) => {
    console.log(data.toString());
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM