MediumπŸ‘€ 3-5 yearsπŸ‘€ 8-15 years 1 min read

Common JSON Parsing Errors β€” Interview Questions

Asked inAmazonMicrosoftCognizantDeloitte
#json#parsing errors#unexpected token#syntax error#debugging
Report issue

⚑ Short Answer

The usual culprits: trailing commas, single quotes or unquoted keys, comments, unescaped control characters or quotes inside strings, wrong encoding/BOM, and truncated payloads ('Unexpected end of input'). A frequent one is calling JSON.parse on something that isn't JSON at all β€” like an HTML error page ('Unexpected token < in JSON').

β˜•Coffee Chat Question

Concept Made Simple

β€œWhat are common JSON parsing errors and their causes?”

🧠Mind Map Answer

Remember It Faster

Unexpected token <β†’parsing an HTML/error page as JSON
Trailing comma→comma after last item
Unexpected end→truncated / empty body
Bad quotes→single quotes / unescaped chars

⌨️Hands-on Keyboard

Learn by Doing

javascript
JSON.parse("{'a':1}");        // ❌ single quotes -> SyntaxError
JSON.parse('{"a":1,}');       // ❌ trailing comma
JSON.parse('');               // ❌ Unexpected end of input
JSON.parse('<html>...');      // ❌ Unexpected token < (got HTML)
JSON.parse('{"a":1}');        // βœ… ok

πŸ”₯What If?

Think Beyond the Expected

Your API call fails with 'Unexpected token < in JSON at position 0' β€” what happened?

You tried to JSON.parse an HTML page, not JSON. Usually the request hit a proxy/error page or a 404/500 that returned HTML, and the client parsed the body blindly. Fix it by checking the HTTP status and Content-Type before parsing, and by logging the raw response body.

πŸ˜‚Real World

'Unexpected token < in JSON' is one of the most-Googled front-end errors β€” it almost always means the endpoint returned an HTML error page. Checking status/content-type before parsing prevents it.

🎯Interviewer's Expectation

Keywords they're listening for:

βœ“ trailing comma / single quotesβœ“ unescaped charsβœ“ truncated bodyβœ“ wrong content (HTML)βœ“ check status/content-type first

⚠️Common Mistakes

  • βœ—Parsing before checking HTTP status/content-type
  • βœ—Assuming a 200 always means valid JSON
  • βœ—Not logging the raw body on failure

βœ…Best Practices

  • βœ“Verify status + Content-Type before parsing
  • βœ“Wrap parsing in try/catch
  • βœ“Log the raw response when parsing fails

πŸ”Follow-up Questions

  • 1How do you handle these errors gracefully?
  • 2Why check Content-Type before parsing?
  • 3How does encoding/BOM cause parse failures?

🧩Related Technologies

HTTPContent-TypeUTF-8/BOMJSON.parse

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly β€” AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: Parsing (JSON)
Interview question: "What are common JSON parsing errors and their causes?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.

Was this answer helpful?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission β€” at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions