juanjoDiaz/streamparser-json

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

mbenedettini opened this issue · 1 comments

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));

Actually I think I found the issue: #45