Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get streamparser/json-node to extract a specific path #44

Open
mbenedettini opened this issue May 8, 2024 · 1 comment
Open

Comments

@mbenedettini
Copy link

The code below doesn't generate any output, where I would expect it to print "this might be a long string". If the path is changed to just $.attachments.0 then it correctly prints this:

$ node src/stream.ts
>>>>>>>> data {
  value: { filename: 'file1', content: 'this might be a long string' },
  key: 0,
  parent: [ <1 empty item> ],
  stack: [
    { key: undefined, value: undefined, mode: undefined, emit: false },
    { key: 'attachments', value: [Object], mode: 0, emit: false }
  ]
}

I'm going through the docs and can't spot the error I'm making, is it in the path I'm using? Thank you!

import { Readable, Transform } from "stream";
import { JSONParser } from "@streamparser/json-node";

const attachmentContentParser = new JSONParser({
  stringBufferSize: 0,
  keepStack: false,
  paths: ["$.attachments.0.content"],
});

const jsonData = {
  attachments: [
    {
      filename: "file1",
      content: "this might be a long string",
    },
    {
      filename: "file2",
      content: "another long string possibly?",
    },
  ],
};
const myJSON = JSON.stringify(jsonData);

const source = new Readable();
source._read = () => {};
source.push(myJSON);
source.push(null);

const reader = source.pipe(attachmentContentParser);
reader.on("data", (data: any) => console.log(">>>>>>>> data", data));
reader.on("error", (error: any) => console.error(">>>>>>>> error", error));
@mbenedettini
Copy link
Author

Actually I think I found the issue: #45

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant