Stop on First Invalid JSON Line
JavaScript
Medium
3 views
Problem Description
First line n and then n lines (any JSON). Parse line by line. If a line is invalid JSON, print BAD k (1-based) and stop. If all ok, print OK.
Input Format
Line1: n. Next n lines: JSON values.
Output Format
OK or BAD k.
Official Solution
const fs=require('fs');const txt=fs.readFileSync(0,'utf8');const lines=txt.split(/\
?\
/);const n=Number((lines[0]||'').trim());if(!n)process.exit(0);for(let i=1;i<=n;i++){const line=(lines[i]||'').trim();try{JSON.parse(line);}catch(e){process.stdout.write('BAD '+String(i));process.exit(0);}}process.stdout.write('OK');
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!