Comments (4)
I didn't understand the business with disassembler()
, so I removed it. The following code works for me:
const fs = require('fs');
const {chain} = require('stream-chain');
const {parser} = require('stream-json');
const {pick} = require('stream-json/filters/Pick');
const {ignore} = require('stream-json/filters/Ignore');
const {streamArray} = require('stream-json/streamers/StreamArray');
const main = () => {
chain([
fs.createReadStream('./data-112.json', {encoding: 'utf8'}),
parser(),
pick({filter: 'result'}),
ignore({filter: /huge/}),
streamArray(),
data => console.log(data)
]);
};
try {
main();
} catch (error) {
console.error('ERROR:', error);
}
data-112.json
:
{
"result": [{"id": 1, "name": "Richard", "huge": "one huge string!"}]
}
It produces the expected result:
// $ node issue-112.js
{ key: 0, value: { id: 1, name: 'Richard' } }
Please let me know if I missed anything significant to your case.
from stream-json.
one huge string
was just an example, you're supposed to replace it with an actual huge string, but I guess I wasn't explicit enough 😄
I published an example repo here, where the script memory is limited to 12mb.
You just have to run npm start
and you'll see the memory issue I'm referencing above
from stream-json.
I see what you mean. You should turn off packing values on Parser
, which is on by default as a convenience. Now the problem is that filters usually operate on keys, so keys are assumed to be small enough to fit memory — they should be packed. Another problem is that streamers assume that individual objects fit the memory so they want all values to be packed. It all means that we need to add some trivial programming:
const fs = require('fs');
const {chain, none} = require('stream-chain');
const {parser} = require('stream-json');
const {pick} = require('stream-json/filters/Pick');
const {ignore} = require('stream-json/filters/Ignore');
const {streamArray} = require('stream-json/streamers/StreamArray');
const packedValues = {endString: 'stringValue', endNumber: 'numberEnd', endKey: 'keyValue'};
const packer = () => {
let accumulator = '';
return function* (chunk) {
switch (chunk.name) {
case 'stringChunk':
case 'numberChunk':
case 'keyChunk':
accumulator += chunk.value;
break;
case 'endString':
case 'endNumber':
case 'endKey':
const value = accumulator;
accumulator = '';
yield chunk;
// add a value token
yield {name: packedValues[chunk.name], value};
return;
}
yield chunk;
};
};
const main = () => {
chain([
fs.createReadStream('./data-112.json', {encoding: 'utf8'}),
parser({packValues: false, packKeys: true}),
pick({filter: 'result'}),
ignore({filter: /huge/}),
packer(),
streamArray(),
data => console.log(data)
]);
};
try {
main();
} catch (error) {
console.error('ERROR:', error);
}
As you can see:
- I told
Parser
to stop packing values save for keys. - Created a trivial packer that glues values together.
stream-json
guarantees that all tokens go in a strict order described in the documentation.
The parser options and the stream of tokens are described in the Parser documentation.
I probably should make it more clear in the documentation. Another idea is to add a packer to streamers and as a stand-alone utility.
from stream-json.
Closing unless a follow up is needed.
from stream-json.
Related Issues (20)
- How to append json in a single file? HOT 2
- can I parse JSON if I have it in a variable? HOT 1
- what about react-native support? HOT 2
- Streaming objects within an object throws JavaScript heap out of memory HOT 1
- [question] Converting array of objects from memory to streamed gzipped JSON, and then reading this content from the db and doing the opposite HOT 4
- Verifier cannot parse input: expected a value. But nothing wrong when inspecting. HOT 4
- Using 'StreamArray.withParser()' with multiple files HOT 3
- Missing data chunks with Writable on async actions HOT 6
- parsing concatenated streams? HOT 3
- Output Error position when reporting the error form the Parser HOT 3
- How do I collect all stream array elements into one array and wait for the stream to end ? HOT 3
- unexpected characters HOT 6
- On Error: Continue to Next Line HOT 5
- Add basic simple example of streaming a large JSON file HOT 5
- Stream an object to reconstruct it on the frontend
- Error from "parent" stream is not propagated to a piped stream. HOT 2
- JSONL parser and Pick HOT 1
- Fatal JavaScript invalid size error 169220804 (see crbug.com/1201626) HOT 2
- JSONL parser throws parsing error that can't be caught using on error or catch block HOT 1
- Can not import StreamArray HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stream-json.