Created
August 27, 2019 04:42
-
-
Save odbol/f66c4ccc4ef1d82565917a464785c3f5 to your computer and use it in GitHub Desktop.
Parse JSON Lines file in the browser with Javascript. http://jsonlines.org/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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