Coder Social home page Coder Social logo

Comments (4)

uhop avatar uhop commented on May 17, 2024

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.

uhop avatar uhop commented on May 17, 2024

Please disregard commits referencing this issue — it was done by mistake.

from stream-json.

kalbert312 avatar kalbert312 commented on May 17, 2024

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.

uhop avatar uhop commented on May 17, 2024

Closing for now.

from stream-json.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.