JSON is one of the most common formats used to exchange data between frontend applications, backend services, and APIs.
It looks similar to a JavaScript object, but JSON has stricter syntax rules.
A small mistake can cause a request body to fail parsing, produce an HTTP 400 response, or prevent automated tests from running correctly.
Here are five common mistakes, how to fix them, and how to catch them before sending an API request.
1. Using single quotes instead of double quotes
Incorrect JSON
{
'name': 'Ram',
'role': 'QA Engineer'
}
JSON requires double quotes around property names and string values.
Correct JSON
{
"name": "Ram",
"role": "QA Engineer"
}
JavaScript permits single-quoted strings in object literals. JSON does not.
2. Adding trailing commas
Incorrect JSON
{
"name": "Ram",
"skills": ["JavaScript", "Playwright"],
}
The comma after the array is not allowed.
Correct JSON
{
"name": "Ram",
"skills": ["JavaScript", "Playwright"]
}
Trailing commas are permitted in many JavaScript object and array literals, but not in standard JSON.
3. Including comments inside JSON
Incorrect JSON
{
// Application configuration
"environment": "test",
"retries": 3
}
Standard JSON does not support comments.
Correct JSON
{
"environment": "test",
"retries": 3
}
Some configuration formats support comments, but those are extensions and should not be assumed to work in a JSON API request.
4. Writing JavaScript values that JSON does not support
Incorrect JSON
{
"user": undefined,
"createdAt": new Date(),
"score": NaN
}
These are JavaScript values or expressions, not valid JSON values.
JSON supports:
- Strings
- Numbers
- Booleans
- null
- Arrays
- Objects
Correct JSON
{
"user": null,
"createdAt": "2026-10-17T10:30:00Z",
"score": 0
}
The corrected values are illustrative. A real application must choose values that accurately represent its data, rather than automatically replacing every invalid value with zero or null.
5. Forgetting to escape special characters
Incorrect JSON
{
"message": "He said "Hello" to me"
}
The internal quotation marks terminate the string unexpectedly.
Correct JSON
{
"message": "He said \"Hello\" to me"
}
Similarly, backslashes and certain control characters need to be escaped correctly.
For example:
{
"folder": "C:\\Users\\Public",
"message": "Line one\nLine two"
}
How to validate JSON using JavaScript
JavaScript provides JSON.parse() for parsing JSON text.
const payload = `{
"name": "Ram",
"active": true
}`;
try {
const data = JSON.parse(payload);
console.log("Valid JSON");
console.log(data);
} catch (error) {
console.error("Invalid JSON:", error.message);
}
If the input is invalid, JSON.parse() throws a SyntaxError.
This is useful when validating JSON received as text.
However, valid JSON syntax does not guarantee that an API accepts the data.
For example, this is valid JSON:
{
"email": 123
}
But an API might require email to be a string.
That is a schema or business-validation issue, not a JSON syntax issue.
How to send JSON with fetch()
When sending JSON in an HTTP request, serialize a JavaScript object using JSON.stringify().
const payload = {
name: "Ram",
active: true
};
const response = await fetch("/api/users", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
console.log(response.status);
Here, /api/users is an illustrative endpoint. Replace it with a valid endpoint for your application.
JSON.stringify() converts the JavaScript object into JSON text.
This helps avoid common errors from manually constructing request bodies.
Remember that JSON.stringify() also has specific behavior for values such as undefined, functions, and non-finite numbers. It is not a substitute for checking that your data matches an API's expected schema.
Quick debugging checklist
Before sending a JSON payload:
- Check that keys and string values use double quotes.
- Remove trailing commas.
- Remove comments.
- Check for unsupported values.
- Escape special characters correctly.
- Validate the JSON syntax.
- Confirm that property names and data types match the API contract.
- Verify the request's Content-Type header when sending JSON.
Final thoughts
JSON mistakes are easy to overlook, especially when working with API automation, JavaScript, and test data.
Understanding the difference between JavaScript object syntax, valid JSON, and API schema validation can save time during debugging.
The most reliable approach is to construct data using normal JavaScript objects, serialize when necessary, validate incoming JSON, and test against the API's expected contract.
This article was prepared with AI assistance and should be reviewed for technical accuracy before publication.
Top comments (0)