Invalid JSON can stop an API request before your application ever reaches its business logic. This guide gives you a repeatable workflow to format JSON online or in an editor, locate syntax errors, validate nested payloads, and prepare reusable request and configuration templates without exposing sensitive data.
Overview
JSON, or JavaScript Object Notation, is a text format used to exchange structured data. APIs commonly use it for request bodies and responses, while configuration files use it to represent settings, feature flags, credentials references, and application options. JSON is deliberately small in scope, but its strict syntax means one missing quote, comma, or brace can make the entire document invalid.
Formatting and validation solve different problems. A JSON formatter adds indentation and line breaks so the structure is easier to inspect. A JSON validator checks whether the text follows JSON syntax and may report an approximate line or column for an error. Neither tool proves that the data is appropriate for a particular API. A document can be valid JSON and still fail because a required field is missing, a value has the wrong type, or the server expects a different schema.
JSON syntax reference
| Element | Valid JSON | Common problem |
|---|---|---|
| Object | {"name": "Ada"} | Using an unquoted key or missing a colon |
| Array | ["draft", "published"] | Leaving out a comma between items |
| String | "status" | Using single quotes instead of double quotes |
| Number | 42 or 19.95 | Adding a currency symbol or trailing comma |
| Boolean | true or false | Writing True, False, or a quoted boolean unintentionally |
| Empty value | null | Writing undefined, which is not valid JSON |
JSON objects use double-quoted property names and colon-separated key-value pairs. Arrays use square brackets and preserve item order. Objects and arrays may be nested, but every opening brace or bracket must have a matching closing character.
Checklist by scenario
When an API payload is rejected
- Copy the request body into a local editor or a trusted format JSON online tool. Avoid pasting passwords, access tokens, personal data, or production secrets into a third-party service.
- Run a JSON validator before changing the content. If it reports a syntax error, fix that error first; server-side validation cannot reliably explain a body that cannot be parsed.
- Compare the payload with the API documentation. Check required fields, exact spelling, nesting, allowed values, and expected data types.
- Confirm the request headers match the body. A JSON request commonly needs an appropriate content type, but the exact requirements belong to the API you are calling.
- Test with the smallest valid payload. Add optional fields one at a time until the failing field or combination is identified.
- Inspect the response status and error body. A parsing error, authentication failure, authorization failure, and schema error require different fixes.
When preparing a nested object
Start with a readable structure rather than compressing everything onto one line:
{
"customer": {
"id": "cus_123",
"contact": {
"email": "ada@example.test"
}
},
"items": [
{
"sku": "book-001",
"quantity": 2
}
]
}
Then check each level from the outside in: the root object, the customer object, the nested contact object, and the items array. A formatter makes this visual inspection much easier, especially when a payload contains several arrays of similar objects.
Reusable REST request template
Use this as a starting point, then replace placeholder values according to the target API. Placeholder strings are still valid JSON, which makes the template easy to validate before you adapt it.
{
"name": "Example project",
"description": "Short description",
"enabled": true,
"labels": ["demo", "internal"],
"owner": {
"id": "user_123"
}
}
Configuration file template
{
"service": {
"port": 8080,
"logLevel": "info"
},
"features": {
"newDashboard": false
},
"database": {
"connectionStringEnv": "DATABASE_URL"
}
}
For configuration files, decide which values belong in JSON and which should remain in environment variables. Store a variable name or reference where appropriate instead of placing a real secret in a committed file. The environment variables and secrets management guide provides a broader checklist for separating configuration from sensitive values.
What to double-check
Syntax errors
These examples look familiar to JavaScript developers but are not valid JSON:
{
'name': 'Ada',
"active": True,
}
The keys and first value use single quotes, True has the wrong capitalization, and the final property has a trailing comma. The valid version is:
{
"name": "Ada",
"active": true
}
Other frequent errors include a missing comma between properties, a missing closing bracket, an unescaped quote inside a string, and a comment such as // temporary setting. Standard JSON does not allow comments. Some editors support JSON-like formats with comments or trailing commas, but those files may not be accepted by a strict API or parser.
Types and representations
Check whether each value is represented as the type the receiver expects. These values are different:
{
"countAsNumber": 5,
"countAsText": "5",
"enabledAsBoolean": true,
"enabledAsText": "true",
"missingValue": null
}
Do not assume that a server will convert strings into numbers or booleans. Also verify date, time, identifier, decimal, and enumeration formats against the API contract. JSON itself does not define an application-specific date type; dates are commonly transmitted as strings, but the accepted format is determined by the receiving system.
Validation boundaries
Separate three checks in your debugging notes:
- Parsing: Is the text valid JSON?
- Schema: Does the structure contain the required fields and permitted types?
- Business rules: Are the values allowed for this user, state, or operation?
A formatter addresses readability, and a syntax validator addresses parsing. API documentation, schema validation, and server responses are needed for the remaining checks. If the failure appears after parsing, review the API's error response rather than repeatedly reformatting the same body. For broader integration troubleshooting, see the API error handling and retry strategy guide.
Common mistakes
- Confusing JavaScript with JSON: JavaScript permits comments, single-quoted strings, variables, and trailing commas in many contexts. JSON does not generally permit these conveniences.
- Fixing only the highlighted character: A parser may point after the real mistake. Inspect the preceding property, comma, quote, or bracket as well as the reported position.
- Sending a formatted response as a request: Copying an API response back into a request can include read-only fields, generated identifiers, or a different object shape.
- Ignoring content type and encoding: Valid JSON can still be delivered incorrectly if the request setup does not match the API's requirements.
- Leaking secrets into online tools: Remove tokens, cookies, credentials, personal information, and internal identifiers before using a browser-based formatter or validator. When in doubt, use a local editor or command-line parser.
- Relying on formatting as validation: A neatly indented payload can still contain the wrong field names, invalid values, or a schema mismatch.
When an error is difficult to isolate, create a minimal reproduction with placeholder values. Keep the original payload private, preserve the failing structure, and add fields back gradually. This produces a smaller and safer example for teammates or issue tracking.
When to revisit
Return to this checklist whenever you prepare a new API integration, change a request schema, migrate configuration between environments, or troubleshoot a parsing error. It is especially useful before deployment and after changing an editor, formatter, validation library, API client, or serialization setting. Tool behavior can differ around comments, trailing commas, encoding, and schema checks, so confirm that the tool's output matches the format accepted by the receiving system.
Before sending a final payload, use this short action list:
- Remove secrets and unnecessary private data.
- Format the JSON for human inspection.
- Validate syntax and resolve the first reported error.
- Compare fields and types with the current API or configuration contract.
- Test the smallest valid example in a non-production environment when possible.
- Record the final shape and any assumptions for the next person who maintains it.
For related developer utilities, use the Markdown preview and formatting guide when documenting payloads, and review the JavaScript debugging workflow when the JSON is generated or consumed by frontend code. A consistent format-and-validate routine turns JSON errors from a vague blocker into a small, traceable debugging task.