JSON is text; a JavaScript object is a runtime value
A JSON document is a sequence of characters that follows a data format. A JavaScript object exists in a running program and can hold values that JSON cannot represent. When an API sends a JSON response, your application normally receives text or bytes and parses them into runtime values.
The similar punctuation is the source of much confusion. You can recognize curly braces and colons in both formats without being able to copy arbitrary JavaScript source into a strict JSON validator.
The key syntax differences
| Feature | Standard JSON text | JavaScript object literal |
|---|---|---|
| Property names | Double-quoted strings | May use identifier names, quoted strings, or computed names. |
| String delimiters | Double quotes | May use single quotes, double quotes, or template literals. |
| Trailing commas | Not allowed | Allowed in many literal forms. |
| Comments | Not allowed | Allowed in source code. |
| Methods and functions | Not representable | Can be properties of an object. |
| undefined, NaN, Infinity | Not JSON values | Exist in JavaScript. |
| Date or Map objects | No native representation | Supported as runtime objects. |
The first block is JavaScript source:
const student = {
name: 'Ada',
active: true,
};The equivalent JSON text contains no variable declaration and uses double quotes:
{
"name": "Ada",
"active": true
}Use JSON.parse and JSON.stringify for the boundary
JSON.parse reads valid JSON text and creates the corresponding JavaScript value. JSON.stringify serializes a supported JavaScript value into JSON text.
const text = '{"name":"Ada","active":true}';
const student = JSON.parse(text);
console.log(student.name); // Ada
const readable = JSON.stringify(student, null, 2);
console.log(readable);The third stringify argument controls indentation. It helps readability, but it does not validate your application’s expected fields. A malformed input document throws a syntax error during parsing; an object with the wrong business fields may still serialize successfully.
Never evaluate JSON as code. Use a JSON parser for data. Avoid eval and dynamic function construction when reading documents from an API or an untrusted source.
Some runtime values change during serialization
JSON has a smaller set of types than JavaScript. Plan how special values should cross your API boundary instead of relying on incidental serialization behavior.
| JavaScript value | Typical JSON.stringify behavior |
|---|---|
undefined in an object property | The property is omitted. |
undefined in an array | The position is serialized as null. |
NaN or Infinity | Serialized as null. |
| A Date instance | Normally serialized as an ISO date string through its toJSON method. |
| A BigInt without custom handling | Throws a TypeError. |
| A circular reference | Throws a TypeError. |
Functions and symbols also need care. A replacer or custom toJSON method can change the result further. Define a documented representation for dates, special numeric values, and domain-specific objects when another system must interpret them.
Why large numbers can change in a formatter
JavaScript’s ordinary Number type cannot exactly represent every long integer. Converting 9007199254740993 to a Number can round it before a formatter has a chance to print the original digits.
Mita’s formatter keeps the original JSON tokens rather than using an ordinary object round trip for its output. This preserves long IDs, decimal spelling, repeated names, and exponent spelling. It also warns about values that can be problematic in a consuming parser.
That is a display and formatting guarantee, not a guarantee about every application reading the document. For identifiers, a documented string representation is often suitable. For calculations requiring greater precision, choose a matching numeric representation and library at both ends of the system.
Choose the right check for the job
Use JSON Formatter & Validator to inspect grammar and layout. Use your code’s tests and API contract to verify required fields and types. Use a separate JSON Schema validator when you need schema-based checks. These checks complement one another: readable text does not automatically mean correct application data.
Put the explanation to work. Open the tool to format, inspect, or validate your document.
Open JSON Formatter →