JSON is so common that it is easy to stop thinking about it. Then one day a deployment fails because of a trailing comma in a config file, an API returns a number that loses precision in JavaScript, or two services disagree about whether a date is in UTC. Most JSON problems are small, but they can be surprisingly expensive.
This article collects practical habits that prevent JSON problems, plus a fast debugging routine for when something breaks. Use the JSON Formatter & Validator alongside these tips. For the basics of formatting and validation, see our JSON formatter guide.
Tip 1: Validate before you commit
Configuration files written in JSON, such as package.json, composer.json, tsconfig.json and many cloud and CI settings, are often edited by hand. One syntax error can stop an application from starting or a build from running.
Make validation a habit: paste the file into the formatter before committing. Better still, use an editor that highlights JSON errors as you type, and add a validation step to your build or CI pipeline.
Tip 2: Memorise the five rules that cause most errors
- Keys must be in double quotes.
- Strings must use double quotes, never single quotes.
- No trailing commas after the last item in an object or array.
- No comments.
- Only valid values: strings, numbers, true, false, null, objects and arrays. No undefined, NaN, Infinity or functions.
Almost every "Unexpected token" error comes from breaking one of these.
Tip 3: Pick a key naming style and stick to it
Mixing userName, user_name and UserName in one API makes code harder to write and easier to break. Choose one convention:
- camelCase is common in JavaScript-heavy systems.
- snake_case is common in Python, Ruby and PHP ecosystems, and in many public APIs.
Document the choice and apply it everywhere. The Case Converter helps rename keys when migrating.
Tip 4: Use ISO 8601 for dates and times
JSON has no date type, so dates are stored as strings or numbers. The safest format is ISO 8601 with a time zone:
"2026-11-24"for a date."2026-11-24T09:30:00Z"for a moment in UTC."2026-11-24T15:30:00+06:00"for a moment with an offset, such as Bangladesh time.
Avoid formats like "24/11/2026", which are ambiguous (is it day/month or month/day?). If you use Unix timestamps, document whether they are in seconds or milliseconds; mixing the two is a classic bug. The Timestamp Converter helps check them.
Tip 5: Beware of large numbers
JSON numbers have no size limit in the specification, but JavaScript stores numbers as 64-bit floating point, which represents integers exactly only up to 9,007,199,254,740,991 (2^53 โ 1). Larger IDs, such as some database IDs or social media post IDs, silently lose precision when parsed in JavaScript.
The common solution is to send large IDs as strings: "id": "1234567890123456789". Many large APIs do exactly this.
Tip 6: Be careful with money
Floating-point numbers cannot represent many decimal fractions exactly, so 0.1 + 0.2 is not exactly 0.3 in most languages. For money:
- Send amounts as integers in the smallest unit, such as paisa or cents (
"amount": 145000for เงณ1,450.00), or - Send them as strings and handle them with a decimal library.
Always include the currency code alongside the amount.
Tip 7: Use null deliberately
Decide what missing data means in your API:
- Omitting a key, and
- Including the key with
null
can mean different things, especially for updates (does null mean "clear this field" or "leave it unchanged"?). Document your convention and apply it consistently.
Tip 8: Keep nesting reasonable
Deeply nested JSON is hard to read and awkward to query. If you find yourself writing data.result.items[0].details.info.value, consider flattening the structure. For querying deeply nested data, the JSONPath Tester lets you test expressions such as $.orders[*].items[*].sku.
Tip 9: Format for humans, minify for machines
Keep JSON in source control and documentation formatted with consistent indentation (two spaces is common), so diffs are readable. Send minified JSON over the network when size matters. The formatter does both in one click.
Tip 10: Sort keys when comparing
When you compare two JSON documents, differences in key order can hide the real changes. Use the JSON Diff tool, which compares structure and values regardless of order, or sort keys before comparing with a plain Text Diff Checker.
Tip 11: Escape strings properly
Inside JSON strings, some characters must be escaped:
| Character | Escape |
|---|---|
| Double quote | \" |
| Backslash | \\ |
| Newline | \n |
| Tab | \t |
| Control characters | \u0000 style |
If you build JSON by hand from user input, use your language's JSON library rather than string concatenation. Manual building is a common source of broken JSON and even security problems. For quick manual escaping, use the String Escape tool.
Tip 12: Never put secrets in client-side JSON
Anything sent to a browser or mobile app can be read by the user. Do not include API secrets, internal tokens or other users' data in JSON responses that reach the client. Also scrub secrets from JSON before pasting it into tickets, chats or public forums.
Tip 13: Version your API responses
When JSON is part of an API that other apps depend on, changing its shape can break them. Removing a key, renaming one or changing a number into a string are all breaking changes. Good practices:
- Add new keys rather than renaming old ones.
- Keep old keys working for a while when you must change them, and announce the change.
- Use versioned endpoints, such as
/api/v1/and/api/v2/, for big changes. - Write down the expected shape of each response, for example with JSON Schema or OpenAPI.
Even small internal projects benefit from this discipline once a mobile app or another team depends on them.
Tip 14: Generate types from sample JSON
If you work in TypeScript, PHP or another typed language, writing data types by hand for a large JSON response is slow and error-prone. Paste a sample response into the JSON to TypeScript tool to generate interfaces, or into JSON to PHP Array for PHP fixtures and tests. Then adjust the generated types for optional fields and edge cases your sample did not include.
Tip 15: Keep test fixtures realistic
Test data that is too simple hides bugs. Include realistic edge cases in your sample JSON:
- Empty arrays and empty strings.
nullvalues where fields are optional.- Very long strings and Unicode text, including Bangla and emoji.
- Large numbers and negative numbers.
- Dates at the end of a month and at midnight in different time zones.
The Fake Data Generator can produce realistic names, emails and addresses to fill fixtures quickly.
A fast debugging routine for broken JSON
When you see a JSON error, work through these steps:
- Paste into the formatter. It points to the line and position of the first error.
- Look just before the reported position. The real mistake is often the character before, such as a missing comma at the end of the previous line.
- Check for trailing commas near closing brackets.
- Check quotes: single quotes, curly "smart" quotes copied from documents, or unescaped quotes inside strings.
- Check the start and end of the file for a byte order mark, stray characters or truncated content.
- Check whether it is really JSON. HTML error pages returned by a server ("โฆ") often cause "Unexpected token <" errors. The real problem is the server error, not the JSON parser.
That last point deserves emphasis: if your code says "Unexpected token < in JSON at position 0", the response is almost certainly an HTML page, such as a 404 or 500 error. Check the HTTP status code and the raw response. Our HTTP status codes guide explains what each code means.
Common JSON mistakes in real projects
- Trailing commas after editing a list.
- Comments added to config files that do not support them.
- Dates without time zones.
- Large IDs as numbers, losing precision in JavaScript.
- Money as floating-point numbers.
- Inconsistent key naming.
- Building JSON with string concatenation.
- Treating an HTML error page as JSON.
Frequently asked questions
Why does my JSON say "Unexpected token <"?
The response is probably HTML, such as an error page, not JSON. Check the status code and the raw response.
How should I store dates in JSON?
As ISO 8601 strings with a time zone, such as "2026-11-24T09:30:00Z".
Can I add comments to a JSON config file?
Standard JSON does not allow comments. Some tools accept JSONC (JSON with comments), but plain JSON parsers will fail.
Why did my large ID change after parsing?
JavaScript cannot represent integers above about 9 quadrillion exactly. Send large IDs as strings.
Should I use JSON or YAML for configuration?
JSON is strict and widely supported, which makes it safe for machines. YAML is easier for humans to read and allows comments, but its indentation rules cause their own errors. Many teams use YAML for hand-edited config and JSON for data exchange. Convert between them with the YAML โ JSON Converter.
Is the JSON Formatter private?
Yes. It runs in your browser and does not upload your data.
Keep reading
How to Format, Validate and Fix JSON Online (JSON Formatter Guide)Developer ยท 7 min read
How to Write a Cron Expression (With a Free Cron Builder)Developer ยท 7 min read
How to Test Regular Expressions Online: A Practical Regex GuideDeveloper ยท 7 min read
How to Decode a JWT and Check Its Expiry (JSON Web Token Guide)Developer ยท 8 min read
How to Encode and Decode Base64 Online (and What It Is Really For)Developer ยท 7 min read
How to Generate UUIDs Online: Version 4 UUIDs ExplainedDeveloper ยท 7 min read
