wardi / jsonlines

Documentation for the JSON Lines text file format
http://jsonlines.org
139 stars 33 forks source link

validator does not accept newline at end #83

Closed allefeld closed 9 months ago

allefeld commented 1 year ago

According to Wikipedia, the Unix convention is that text "lines are sequences of zero or more non-newline characters plus a terminating newline character". "\n" is therefore not a line separator, but a line terminator. This convention is also enforced by some text editors, which add a newline at the end of the file if there isn't one.

Your description of JSON Lines is lenient in that respect: "The last character in the file may be a line separator, and it will be treated the same as if there was no line separator present."

However, your validator does not accept lines like this. If I enter e.g. "42", that's declared valid jsonl, but "42\n" gives "SyntaxError: Unexpected end of JSON input".

wardi commented 1 year ago

jsonlines.org also says "The last character in the file may be a line separator, and it will be treated the same as if there was no line separator present." so yes this looks like something worth fixing.

leoalho commented 1 year ago

Hi i'm not affiliated with jsonlines.org but I made a PR (#84) which should fix the issue. In the current version in ./javascript/validator.js there is an if statement in the loop that iterates over the lines: if (i !== lines.length - 1 || lines !== ''){. This to my understanding doesn't do anything because lines is an array, so lines !=='' is always true. I replaced it with if (!(i === lines.length - 1 && lines[i] === '')) { which should now correctly skip the json parsing if the last line is empty

sp4ce commented 9 months ago

Thank you for the help! I merged it. Should be live now. Closing.