Read JSON Config

Read JSON Config

Medium NodeJS File System & Paths 35 views
Explanation Complexity

Problem Statement

Read config.json and print config.site or 'meetcode'.

Input Format

No input.

Output Format

Print one word.

Constraints

If file missing or invalid JSON, fallback.

Input / Output Format

Input Format
No input.
Output Format
Print one word.
Constraints
If file missing or invalid JSON, fallback.

Examples

Input:
Output:
meetcode

Example Solution (Public)

NodeJS
const fs = require('fs');
let site = 'meetcode';
try {
  const txt = fs.readFileSync('config.json', 'utf8');
  const obj = JSON.parse(txt);
  if (obj && typeof obj.site === 'string' && obj.site.trim()) site = obj.site.trim();
} catch (e) {}
console.log(site);

Official Solution Code

const fs = require('fs');
let site = 'meetcode';
try {
  const txt = fs.readFileSync('config.json', 'utf8');
  const obj = JSON.parse(txt);
  if (obj && typeof obj.site === 'string' && obj.site.trim()) site = obj.site.trim();
} catch (e) {}
console.log(site);
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.