Comments (4)
I am not sure what you want to achieve. So I created this JSON file:
{
"id": 2,
"name": "Some Name",
"extensions": ["Ext1", "Ext2"],
"blah1": 1,
"blah2": 2,
"blah3": 3
}
Wrote this program using your code:
const fs = require('fs');
const {chain} = require('stream-chain');
const {parser} = require('stream-json');
const {filter} = require('stream-json/filters/Filter');
const {streamObject} = require('stream-json/streamers/StreamObject');
const pipeline = chain([
fs.createReadStream('./subset.json'),
parser(),
filter({filter: /^(?:id|name|extensions)\b/}),
streamObject(),
data => console.log(data)
]);
And it produced:
{ key: 'id', value: 2 }
{ key: 'name', value: 'Some Name' }
{ key: 'extensions', value: [ 'Ext1', 'Ext2' ] }
I guess using the rest of your code (Promise
, and result[data.key] = data.value
) it will create a subset of your original object from three named properties. Is this what you want to do?
Another way to do it is to parse everything into a token stream, edit it with Filter
, Ignore
, Replace
, Pick
, and then pipe it either to Assembler
, which creates an object, or Stringer
, which can pipe the resulting JSON back to a file or any other text stream.
The example based on the previous program:
const fs = require('fs');
const {chain} = require('stream-chain');
const {parser} = require('stream-json');
const {filter} = require('stream-json/filters/Filter');
const Assembler = require('stream-json/Assembler');
const pipeline = chain([
fs.createReadStream('./subset.json'),
parser(),
filter({filter: /^(?:id|name|extensions)\b/})
]);
const asm = Assembler.connectTo(pipeline);
pipeline.on('end', () => console.log(asm.current));
It produced:
{ id: 2, name: 'Some Name', extensions: [ 'Ext1', 'Ext2' ] }
One possible improvement is to check, when you have all required properties (in this case id
, name
, and extensions
) and return a formed object earlier ignoring the rest. This way you don't need to wait until the whole file is finished processing.
Let me know if you are on the way to a solution. Don't forget to check docs and examples in the wiki of this repository.
from stream-json.
Please disregard commits referencing this issue — it was done by mistake.
from stream-json.
Thank you I will try this again. For some reason using my code, the extensions
array was an empty array despite the file having a couple values. I'll double check what's going on.
from stream-json.
Closing for now.
from stream-json.
Related Issues (20)
- createWriteStream writes whitespace instead of data HOT 1
- 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
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.