Missing Key Finder
JavaScript
Easy
3 views
Problem Description
Input has a JSON object in line1 and a key in line2. If key is present (own property), print its value as JSON. Else print MISSING.
Input Format
Line1: JSON object. Line2: key.
Output Format
One line output.
Sample Test Case
Input:
{"a":5,"b":{"x":1}}
b
Constraints
Total input length up to 2e5.
Official Solution
const fs=require('fs');const txt=fs.readFileSync(0,'utf8');const lines=txt.split(/\
?\
/);const oLine=(lines[0]||'').trim();const key=(lines.slice(1).join('\
')||'').trim();if(!oLine||!key)process.exit(0);try{const obj=JSON.parse(oLine);if(!Object.prototype.hasOwnProperty.call(obj,key))throw new Error('MISSING');process.stdout.write(JSON.stringify(obj[key]));}catch(e){process.stdout.write('MISSING');}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!