Skip to content

Instantly share code, notes, and snippets.

@odbol
Created Aug 27, 2019
Embed
What would you like to do?
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