r/learnprogramming • u/davidbuilds2208 • May 02 '26
Why does valid-looking JSON still break things sometimes?
I ran into something recently that confused me a bit.
I had some JSON from an API response that looked totally fine at first glance, but it kept breaking things in my app.
Turned out there were small issues like missing quotes or a trailing comma — stuff that’s easy to overlook.
What confused me is that everything else seemed “normal” (no obvious errors until it actually failed somewhere downstream).
I feel like I keep running into this where JSON looks okay but isn’t actually valid.
How do you usually deal with this?
Do you just debug it manually every time, or is there a better way to catch/fix these issues early?
7
u/Organic-Afternoon-50 May 02 '26
Are you using an Agentic AI Coding assistant? If so, There's a few that constantly have this issue.. repeatedly breaking and fixing it.
5
u/szank May 02 '26
So you are saying that an invalid json still can look valid at a glance? yes it can. just like improperly indented python code or yaml or c++ that's missing a bloody semicolon somewhere.
Still invalid. Linting helps.
3
u/Any-Range9932 May 02 '26 edited May 02 '26
Linting the answer. If the actual api endpoint is malformed, file a bug with them if you dont have access to it. And if you need to still use it, you can transform it to fit your use case
2
u/BuiltByEcho May 03 '26
I'd stop treating it as an eyeballing/debugging problem and make JSON validation part of the boundary where data enters your app.
A practical setup:
One important distinction: if the API is returning trailing commas or missing quotes, that's not JSON with quirks — it's invalid JSON. Don't try to "clean it up" with regex unless you're forced to deal with a broken provider. Better to fail fast at the edge and surface a clear error.
12
u/0x14f May 02 '26
https://jsonlint.com
4
u/r_hayess May 02 '26
Man, the trailing comma is the ultimate silent killer in JSON! We’ve all been there. JSON is very strict, so even one tiny character out of place makes the whole object unparseable. To stop debugging this manually, here is what pro devs use:
Linters & Formatters: If you use VS Code, install extensions like 'JSON Prettier' or 'ESLint'. They will highlight syntax errors in red before you even run the code.
JSON Validation Tools: Use online tools like JSONLint or browser extensions that format JSON responses. If it doesn't format correctly there, it’s not valid.
Schema Validation: For bigger projects, use something like JSON Schema. It doesn't just check if the JSON is 'valid', but also if it has the right structure (e.g., 'Does this user object actually have an email string?').
Try-Catch Blocks: Never assume an API response is valid. Always wrap your parsing logic in a try-catch block:
Stop looking with your eyes and let the tools do the work
2
u/13oundary May 02 '26
Why is JSON that's wrong wrong? I'm confused? What are you interacting with that produces wrong JSON that you expect not to?
2
u/jcunews1 May 03 '26
Having valid JSON syntax doesn't mean having valid JSON content.
1
u/TasteMedical5032 May 06 '26
This usually happens because “looks like JSON” and “is valid JSON” are not the same thing.
Small things like trailing commas, missing quotes, single quotes, comments, invisible characters, or AI/API responses wrapped in extra text can make it fail once another system tries to parse it.
The better approach is not to debug it manually every time, but to add a validation step before the data reaches the rest of the app.
Basic flow:
So instead of letting broken JSON fail somewhere later, you catch it at the boundary where it enters the system.
For quick checks, JSON validators help. For real workflows, schema validation is better because valid JSON can still be structurally wrong for your app.