FrontendAtlas
Interview Questions
Home>JavaScript interview questions> JavaScript coding challenges >Safe JSON Parse (Fallback on Error)

Safe JSON Parse (Fallback on Error)

easy
By FrontendAtlas Editorial · Updated Feb 3, 2026
Implement safeJsonParse(text, fallback = null) to safely parse JSON. It should return the parsed value for valid JSON strings and the fallback for invalid JSON or non-string inputs. This is a common edge-case strategy for parsing unknown inputs.

Arguments

  • text: string — The JSON string to parse.
  • fallback: any — Value to return when parsing fails. Defaults to null.

Returns

any — Parsed JSON value or the fallback.
Examples
safeJsonParse('{"ok":true}') // => { ok: true }

safeJsonParse('not json', []) // => []

safeJsonParse(42, { ok: false }) // => { ok: false }

Solution

Overview

Guard the input type, then try JSON.parse in a try/catch. On any error, return the fallback. Keep the behavior predictable so tests can cover invalid inputs and boundary cases.

1

Approach: Guard + try/catch

Steps:

1) If text is not a string, return fallback.

2) Attempt JSON.parse.

3) On error, return fallback.

Complexity: O(n) time for parsing, O(1) extra space.

export default function safeJsonParse(text, fallback = null) {
  if (typeof text !== 'string') return fallback;
  try {
    return JSON.parse(text);
  } catch {
    return fallback;
  }
}
export default function safeJsonParse(text: string, fallback: any = null): any {
  if (typeof text !== 'string') return fallback;
  try {
    return JSON.parse(text);
  } catch {
    return fallback;
  }
}

Notes & Pitfalls

Pitfalls
  • Assuming JSON.parse always returns an object (it can return numbers, strings, booleans, or null).
  • Catching errors but returning undefined, which makes tests ambiguous.
  • Parsing non-strings without guarding can throw or behave unexpectedly.
Edge cases
  • Valid primitives like '0', 'true', and 'null' should parse correctly.
  • Whitespace around JSON is valid and should parse.
  • Invalid JSON should always return the fallback without throwing.
Techniques
  • Type guards before parsing.
  • Deterministic fallback behavior for tests.
  • Focused unit tests per edge case.

Resources

  • MDN – JSON.parse()

Similar questions

Validate Username (Rules + Edge Cases)easySanitize href URL (Block javascript: XSS)intermediateFormat Date in Time Zone (YYYY-MM-DD)intermediate

Guides

Frontend interview preparation guideGuideFrontend coding interview questions and prep guideBlueprintJavaScript Problems That Actually Show UpBlueprintBuild Great UI in 60 MinutesBlueprint

Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.

Open frontend interview questionsBrowse JavaScript interview questionsOpen Essential 60Open Machine Coding HubOpen Frontend Coding ChallengesOpen System DesignOpen Interview Prep GuideOpen System Design BlueprintOpen Framework Prep PathsOpen Study PlansOpen JavaScript mastery study planOpen Company PrepOpen JavaScript Framework Prep Guide
↗Incidents hub
← Prev←83 / 88