Skip to content

Instantly share code, notes, and snippets.

@odbol
Created August 27, 2019 04:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odbol/f66c4ccc4ef1d82565917a464785c3f5 to your computer and use it in GitHub Desktop.
Save odbol/f66c4ccc4ef1d82565917a464785c3f5 to your computer and use it in GitHub Desktop.
Parse JSON Lines file in the browser with Javascript. http://jsonlines.org/
function parseJsonLines(file) {
let results = [];
for (let i = 0; i < file.length;) {
const end = file.indexOf('\n', i);
if (end < 0) {
end = file.length;
}
const currentLine = file.substring(i, end);
try {
results.push(JSON.parse(currentLine));
}
catch (e) {
console.error('Skipping broken line ' + currentLine);
}
i = end + 1;
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment