Malformed JSON can stop an API request, break a configuration file, or hide the real cause of a frontend bug. This guide provides a repeatable workflow for validating and formatting JSON, locating syntax errors, checking data types, and reviewing request or response payloads with browser-based developer tools and command-line utilities.
Overview
JSON is a text format built from objects, arrays, strings, numbers, Boolean values, and null. Its rules are deliberately small, but a single missing quotation mark or trailing comma can make an entire document invalid. Formatting improves readability; validation confirms that the text can be parsed. These are related tasks, but they are not identical.
A JSON formatter typically parses valid input and displays it with indentation. A JSON validator or syntax checker identifies where parsing fails. Some browser-based developer tools also offer minification, key sorting, tree views, and conversion between JSON and other representations. Treat online tools as convenient debugging aids, not as a place to paste credentials, access tokens, personal data, or production secrets.
For a reliable workflow, preserve the original payload, make a copy for editing, validate the smallest useful sample, and then test the corrected data in the application that will consume it. If the document is valid JSON but still rejected by an API, the problem may be a schema, authorization, content type, endpoint, or business-rule issue rather than JSON syntax.
Checklist by scenario
When you need to format JSON online
- Copy the payload into a trusted JSON formatter.
- Check whether the tool reports a line and column for an error before attempting to format.
- Use the formatted tree or indentation to inspect nesting and repeated structures.
- Compare the result with the original text before saving changes.
- Revalidate the final version after every manual edit.
Formatting is especially useful when reviewing an API response, comparing configuration files, or preparing a pull request. A consistent indentation style makes changes easier to spot, but indentation itself does not change the data.
When an API request or response fails
- Capture the exact request body or response body, including the reported status and headers.
- Confirm the body is actually JSON and that the request uses the expected
Content-Type, commonly an application-specific JSON media type. - Run the body through a JSON validator before investigating application logic.
- Check required fields, field names, nesting, and expected data types against the API documentation or schema.
- Send a minimal valid payload, then add optional fields one at a time.
A valid JSON document can still be an invalid API payload. For example, an endpoint may require userId as a number while the request sends "userId": "42". The parser accepts both forms, but the application may not.
When repairing imported or hand-written JSON
- Identify whether the source is JSON or a similar format such as JavaScript object syntax, JSON5, YAML, or a log representation.
- Replace single-quoted strings with double-quoted JSON strings.
- Remove comments and trailing commas if strict JSON is required.
- Escape characters inside strings correctly.
- Validate after each logical correction rather than changing the whole file at once.
If the source contains expressions, variables, functions, or comments, it may be configuration written in another language rather than JSON. Renaming the file is not enough; convert the content deliberately.
When using the command line
For repeatable checks, add a parser to your local workflow. Many development environments provide a command-line JSON processor or a language runtime that can parse a file and return an error location. A simple parse check can be used before a commit or deployment, while a formatter can produce a consistent output for review. The exact command depends on the operating system and toolchain, so keep the chosen command in project documentation or a package script.
What to double-check
| Area | Valid JSON rule | Typical failure |
|---|---|---|
| Object keys | Keys are strings enclosed in double quotes. | {name: "Ada"} instead of {"name": "Ada"}. |
| Strings | Use double quotes and escape embedded characters. | An unescaped quote ends the value early. |
| Separators | Use commas between members or array items. | A missing comma joins two values incorrectly. |
| Trailing commas | Do not leave a comma after the final item. | JavaScript may accept it while strict JSON parsing fails. |
| Nesting | Every object and array must close in the correct order. | ] is used where } is required. |
| Numbers | Use JSON number syntax, without language-specific values. | NaN, Infinity, or a numeric expression is included. |
| Boolean values | Use lowercase true or false. | True or "false" is sent unintentionally. |
| Null | Use lowercase null when a null value is intended. | An empty string, omitted property, and null are treated as interchangeable. |
Pay particular attention to escape sequences. Within a JSON string, a quotation mark, backslash, or control character may need escaping. A URL can be a valid string without URL-encoding, but it must still follow JSON string rules. Similarly, a base64 value is still just a string to JSON; encoding does not make the surrounding document valid.
Validation and schema checking answer different questions. Syntax validation asks, “Can a JSON parser read this?” Schema validation asks, “Does this parsed value have the required fields, types, ranges, and structure?” Use both when an API, event stream, or configuration system defines a formal contract.
Common mistakes
Confusing JavaScript with JSON
JavaScript object literals can contain unquoted keys, comments, variables, and trailing commas. Strict JSON cannot. A formatter may reject JavaScript-like input even though the text looks familiar. Decide which format the receiving system expects before making corrections.
Fixing only the first visible error
Parsers often stop at the first point where the document becomes impossible to interpret. Correcting that point may reveal a second error later in the file. Re-run validation after each correction and work from the earliest reported location.
Changing values while fixing syntax
Adding a missing quote is a syntax change; changing 42 to "42" changes the data type. Keep those decisions separate. If an API requires a string, make that change intentionally and record why.
Trusting visual formatting
Indented text can look correct while containing an invalid escape, duplicate key, unexpected type, or wrong nesting level. Always parse the final text. Also check how the target parser handles duplicate object keys, because relying on one key silently replacing another can conceal data loss.
Pasting sensitive payloads into public tools
Before using a browser-based JSON validator, remove authentication headers, cookies, tokens, private customer data, and internal hostnames when they are not necessary for diagnosis. For sensitive or production data, use a local formatter or an approved internal utility.
When to revisit
Return to this checklist whenever an API contract changes, a configuration file is migrated, a parser or formatter is replaced, or a deployment workflow is updated. It is also useful before a seasonal planning cycle if scheduled jobs, integrations, or data imports are being revised. Tool behavior can differ around duplicate keys, schema drafts, number handling, Unicode, and non-standard extensions, so confirm the behavior that matters to your project rather than assuming every validator is identical.
Before committing or deploying, use this final sequence:
- Preserve the original input and redact sensitive values.
- Parse the complete document with the same language or tool family used in production when possible.
- Format the parsed result consistently for review.
- Validate the schema, required fields, and data types.
- Test the smallest representative API request or configuration path.
- Review the diff for unintended value changes, missing fields, and secrets.
- Record the formatter, validator, or project command so the next person can repeat the check.
For related debugging work, see the HTTP Headers Reference when the payload appears correct but the request still behaves unexpectedly. If JSON is part of a wider deployment process, the cloud deployment checklist can help connect payload validation with secrets, DNS, and rollback checks.