On this page

To distinguish JSON text from a JavaScript object literal, check property names, string quotes, values, comments, and trailing commas. JSON requires double-quoted property names and strings, permits a defined set of data values, and excludes comments and trailing commas. JavaScript object literals support additional syntax, including unquoted identifier keys, computed names, and function-valued properties.
Some snippets satisfy both syntaxes. The text {"name":"Ada"} is valid JSON and also fits JavaScript object literal syntax when used in an expression context. Its appearance therefore cannot tell you whether someone intends to exchange text or initialize an object in JavaScript. This guide pairs small examples with a practical workflow for deciding what you have and whether to parse it, serialize a value, or check its JSON syntax.
Similar appearance, different roles
JSON is a text-based, language-independent format for exchanging data. A JavaScript object initializer, commonly called an object literal, is an expression that describes the initialization of an object. Those roles explain why the same characters can appear in both places without making the two concepts interchangeable.
Consider this snippet:
{"name":"Ada","active":true}
As JSON text, it represents an object containing a string and a boolean. In a JavaScript expression context, the same object syntax can describe an object initializer. Double-quoted keys do not make it exclusively JSON.
A JavaScript example makes the context explicit:
const profile = {"name":"Ada","active":true};
The object portion overlaps with JSON syntax. The complete declaration is JavaScript source; it is not a JSON text to submit to a JSON parser. When inspecting an example, identify whether you are classifying the object portion or the entire supplied input.
Ask two separate questions
First ask, “Does this exact text satisfy JSON syntax?” Then ask, “How is this snippet being used?” The first question concerns syntax. The second concerns context: perhaps the text is an input to parse, or perhaps an object initializer appears inside JavaScript code.
| Observation | What it establishes |
|---|---|
| Every key and string uses double quotes | One JSON requirement is satisfied |
| An identifier key is unquoted | The object is not strict JSON |
| The object satisfies both sets of syntax rules | Context still determines its role |
| A surrounding declaration contains the object | Inspect the object separately from the complete JavaScript statement |
Treat visual inspection as a way to find decisive differences. If no difference rules JSON out, continue checking the remaining syntax rather than deciding from braces alone.
Start with property names and string quotes
Property names and string delimiters are a useful first pass because small changes can clearly separate the syntaxes. JSON object property names must be strings enclosed in double quotes. JSON string values also require double quotes.
Compare these examples:
{"name":"Ada"}
{name:'Ada'}
The first satisfies the JSON requirements shown here. The second uses an unquoted identifier key and a single-quoted string value, so it is not JSON. Both features belong in the JavaScript comparison, but each should be checked independently.
Isolate the reason for rejection
These two examples separate the quote issues:
{name:"Ada"}
{"name":'Ada'}
In the first, the property name rules out JSON. In the second, the string delimiter rules it out. Correcting only one feature in {name:'Ada'} would therefore leave another JSON syntax problem.
JavaScript object initializers also provide forms that do not correspond to JSON property definitions:
const name = "Ada";
const shorthand = {name};
const computed = {["name"]: "Ada"};
The shorthand form is not permitted in JSON. The computed form uses an expression in brackets as its property name, a feature supported by JavaScript object initializers. For JSON text, express the intended data with an explicit double-quoted name and a permitted value:
{"name":"Ada"}
When reviewing a larger snippet, examine names and values separately. A quoted key does not establish that its value is valid JSON, and a double-quoted string value does not repair an unquoted key. This makes a two-pass review useful: inspect property definitions first, then inspect the values they contain.
Check what the values represent
JSON can represent strings, numbers, booleans, null, objects, and arrays. A property name can be correctly quoted while its value still makes the entire input invalid JSON.
This example uses permitted value types:
{
"name": "Ada",
"count": 3,
"active": true,
"note": null,
"tags": ["example", "profile"],
"settings": {"compact": false}
}
Apply the same review inside arrays and nested objects. Finding valid values near the beginning does not establish that every later value follows the rules.
Values that rule out JSON
Compare an explicit null with an unsupported value:
{"note":null}
{"note":undefined}
The first is valid JSON. The second is not, because undefined is not a permitted JSON value. Do not automatically replace it with null simply to pass a checker. Ask which representation the receiving application expects before changing the data.
A function provides another clear distinction:
{"getName": function () { return "Ada"; }}
A JavaScript object property can refer to a function, but JSON does not permit functions as values. If the intended payload is a name, ask whether the data should contain that name explicitly. Turning behavior into a data representation requires a decision about what should be exchanged.
Keep NaN and Infinity separate from ordinary numbers
Although JSON supports numbers, NaN and Infinity are not permitted JSON values:
{"score":NaN}
{"limit":Infinity}
Both examples fail the JSON value requirement. Quoting these tokens would produce strings, so that edit should only be made if strings are the intended data.
| Value shown | Permitted in JSON? |
|---|---|
"Ada", 3, true, null |
Yes |
[], {} |
Yes |
undefined |
No |
| A function | No |
NaN, Infinity |
No |
The practical question is both syntactic and semantic: can JSON represent this value, and does the chosen representation express what the recipient needs?
Look for comments and trailing commas
Comments and trailing commas can disqualify text even when every property name and value otherwise looks suitable for JSON. Check them separately so the reason for rejection remains clear.
Comments outside strings
JSON syntax does not allow comments. Compare:
{"name":"Ada"}
{
"name": "Ada" // display name
}
The added comment makes the second example unsuitable as JSON text. The property name and string value did not change; the surrounding syntax did.
Comment-like characters inside a string are a different case:
{"note":"The characters // are part of this text"}
Here, the slashes belong to a double-quoted string value. Their appearance does not create an actual comment. When reviewing text manually, ask whether the characters occur inside the string or outside it. Searching for slash characters alone is not enough to classify the input.
A comma after the final item
JSON objects and arrays cannot contain trailing commas. These pairs isolate the difference:
{"name":"Ada"}
{"name":"Ada",}
And for an array:
["Ada","Lin"]
["Ada","Lin",]
In each second example, the comma after the final item rules out JSON. The object example illustrates a trailing comma in JavaScript object syntax, but that does not make it acceptable to a JSON checker.
If an input contains both a comment and a trailing comma, removing one leaves the other problem in place. Make a focused correction, then check the complete input again. This keeps a small visible fix from becoming an unsupported assumption that everything else is valid.
Do not assume JSON must start with a brace
A JSON text is a serialized value, so it is not restricted to an object or array. The object-focused comparisons in this guide explain a common source of confusion, but they do not describe every possible JSON text.
Each line below is a separate complete JSON text:
["Ada","Lin"]
"Ada"
42
true
null
The array represents a collection; the remaining examples represent individual primitive values. An input does not need property names to qualify as JSON.
Separate format validity from expected shape
Suppose an application expects an object containing a name property. The JSON text "Ada" can be syntactically valid while still being the wrong shape for that application. That mismatch should prompt a check of the receiving system’s requirements, rather than a claim that strings cannot be complete JSON texts.
An obsolete specification restricted JSON texts to objects or arrays, which can explain references that describe a narrower rule. For this guide’s comparison, use the broader rule: a serialized JSON value can stand on its own.
A useful review sequence is therefore to establish syntax first and expected shape second. Ask whether the input is valid JSON, then whether the recipient expects an object, an array, or another permitted value. Adding braces merely to make an input look more familiar changes its structure and should follow an actual data requirement.
Choose the next step: parse, serialize, or check syntax
Once you know whether you have text or a JavaScript value, choose the operation that matches your starting point. Parsing, serialization, and syntax checking answer different questions.
| Starting point | Intended result | Next step |
|---|---|---|
| JSON text | The value represented by that text | JSON.parse() |
| A value to serialize | A JSON string representing the value | JSON.stringify() |
| Text whose JSON syntax is uncertain | A syntax assessment | A JSON syntax checker |
Parse text to obtain its value
JSON.parse() parses text as JSON and returns the produced value:
const text = '{"name":"Ada"}';
const value = JSON.parse(text);
The single quotes here delimit a string in the surrounding JavaScript source. Inside that string, the JSON property name and string value use double quotes. Distinguish the JavaScript code holding the text from the text passed to the parser.
Use parsing when the input is JSON text and you need the value it represents. Do not treat the parser as a way to accept arbitrary JavaScript object literal syntax.
Serialize a value to produce JSON text
JSON.stringify() provides serialization of a specified value to a JSON string:
const value = {name: "Ada"};
const text = JSON.stringify(value);
Here, the starting point is a JavaScript value. That differs from taking a piece of source text and checking whether its characters already form valid JSON. If the source contains functions or unsupported values, first decide what data should be represented; do not assume serialization supplies the application meaning you intended.
Use FormatBits for the syntax-checking step
FormatBits is a place to check JSON syntax within a developer data-formatting workflow. Use a syntax check to assess the exact text you intend to exchange. Product behavior has not been independently verified for this guide, so confirm the current interface and its reported results when using it.
A successful syntax check establishes that the submitted text satisfies the checked syntax rules. It does not establish that the payload contains the fields, value constraints, or overall shape your application requires. Those questions need schema or application validation against the actual requirements. Do not assume a JSON syntax checker also performs that validation.
Before handing off data, ask three practical questions: Does this exact text satisfy JSON syntax? Does its structure match the recipient’s expectations? Do its values carry the intended meaning? Keeping those questions distinct makes the result more useful than a syntax verdict alone.