Why a JSON Minifier Should Validate First
Learn why JSON should be parsed before minifying, and how to troubleshoot trailing commas, single quotes, comments, brackets, and string escaping.
Learn why JSON should be parsed before minifying, and how to troubleshoot trailing commas, single quotes, comments, brackets, and string escaping.
Spaces inside JSON strings can be real data. A global whitespace replacement cannot reliably tell layout whitespace from content. Parsing JSON first makes that distinction before serializing compact output.
Direct cleanup can also hide copied fragments, missing brackets, or comments. A minifier should stop on invalid input instead of producing a result that looks usable but is not valid JSON.
A frequent mistake is JavaScript object syntax instead of JSON: {'state':'ready',} fails because keys/strings use single quotes and the last member has a trailing comma. Another common issue is an incomplete structure such as {"items":[1,2} with mismatched closing characters.
String escaping can fail when a copied value contains a raw line break, backslash, or quote. Reduce the input to the smallest failing fragment; after fixing {"note":"line 1 line 2"} to use the two-character \n escape, rerun validation before minifying.
| Symptom | Likely cause | What to check |
|---|---|---|
| Unexpected token | Trailing comma, comment, or extra text | Remove non-JSON syntax |
| Unexpected end | Copied fragment or unclosed bracket | Complete the object, array, or string |
| Bad string | Quote or escape problem | Check double quotes, backslashes, and line breaks |
First confirm the top-level value is an object, array, string, number, boolean, or null. Then check that object keys and strings use double quotes, fields have commas between them, and arrays or objects close correctly.
If the content came from logs or code, remove prefixes, timestamps, comments, and wrapper text so only the JSON fragment remains. Format the fragment to review structure, then minify it for copying.
0FAQ
Standard JSON does not include comments. Some configuration formats allow comments, but they are not strict JSON and cannot be handled directly by JSON.parse.
JSON strings and object keys must use double quotes. Single quotes belong to JavaScript syntax, not standard JSON syntax.
It is useful, but not always the exact root cause. Missing commas or brackets can make the parser report a position after the real mistake.
Further reading
Keep exploring