NadirTools

Strict JSON Syntax and Formatting Rules

1 min read

Understand the strict structural requirements of JSON to avoid common parsing errors.

The Strict Rules of JSON

JavaScript Object Notation (JSON) is the undisputed king of lightweight data-interchange formats. However, because it is designed for rapid machine parsing, it enforces strict syntax rules that easily trip up developers:

1. **Double Quotes Only**: All keys and string values must be enclosed in double quotes (`"`). Single quotes (`'`) or backticks (`` ` ``) are invalid and will cause immediate parsing failures.

2. **No Trailing Commas**: Unlike standard JavaScript objects, JSON strictly forbids a trailing comma after the final property in an object or the final element in an array.

3. **Data Types**: Allowed data types are String, Number, Object (JSON object), Array, Boolean, and Null. You cannot serialize Functions, `undefined`, or Date objects (Dates must be converted to ISO 8601 strings).

The Cost of Syntax Errors

A single misplaced comma in a massive JSON payload will cause `JSON.parse()` to throw a fatal exception. In edge environments or microservices, unhandled parse exceptions can crash entire node processes.

Frequently Asked Questions

Q: Can I use single quotes in JSON?

No. JSON syntax strictly requires double quotes for all keys and string values. Single quotes will cause a parsing error.

Q: Does JSON support comments?

No. Standard JSON does not support comments (like // or /* */). If you need comments for configuration files, consider using JSONC or YAML.

Q: Can I store a Date object in JSON?

JSON does not have a native Date type. Dates must be serialized into Strings (usually ISO 8601 format) before being placed in a JSON payload.