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

Extra characters in written file #44

Closed
gwc4github opened this issue Mar 7, 2016 · 5 comments
Closed

Extra characters in written file #44

gwc4github opened this issue Mar 7, 2016 · 5 comments

Comments

@gwc4github
Copy link

Hello JP. Thanks for providing you jsonfile package.
I have been trying to use your jsonfile package and I am having an interesting problem.
This code:
jsonfile.writeFileSync('/tmp/gregg.dat', '{ "type":"configuration/entityTypes/HCP", \n"attributes": {\n');
Writes this:
"{ \"type\":\"configuration/entityTypes/HCP\", \n\"attributes\": {\n”
instead of:
{ "type":"configuration/entityTypes/HCP”, “attributes”: {

Am I doing something wrong?

@jprichardson
Copy link
Owner

Remove the '... i.e. you're writing a string instead of an object. Please reopen if you're still unsure of what's going on.

@gwc4github
Copy link
Author

Thanks JP. That does allow me to create some files but only complete objects. However, the JSON I am generating appears to be too big and kills writeFileSync(). Or maybe there is another issue I am not yet aware of. I can format the JSON in a text editor so I think it is ok. Here is the error:
_stream_writable.js:260
var len = state.objectMode ? 1 : chunk.length;
^
TypeError: Cannot read property 'length' of undefined

Would you consider adding a flag so that we can use it to write text? That way I can build the JSON bit-by-bit. I suppose others might want to use if for text files too?

Thanks again JP

@jprichardson
Copy link
Owner

That does allow me to create some files but only complete objects.

What do you mean by "complete objects"? Do your objects contain a large amount of data? Are you trying to write out multiple objects, one per line?

@gwc4github
Copy link
Author

I am building the objects from metadata and data that I am getting from other sources. So, this string is the start of a large object that I want to generate but is not yet complete:
"'{ "type":"configuration/entityTypes/HCP", \n"attributes""
So, I could have tried to write the code to keep adding to objects and arrays instead of building a string but that was harder to do in some cases. Maybe I should have done it that way anyway but this seems easier to debug?
the code currently creates a large object in memory as a string. I tried writing that out with your function as shown and that didn't work. I also tried the standard fs.createWriteStream() and right now that fails too. It's about 49k in size.

@jprichardson
Copy link
Owner

If you're writing large amounts of data, fs.createWriteStream() is what you want. If that's failing, there's some other issue going on.

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

2 participants