A JSON formatter can make structured data readable, but formatting alone will not explain every failure. This guide presents a repeatable workflow for formatting, validating, and debugging JSON, from the first syntax error through API testing and safe handoffs.
Overview
JSON, short for JavaScript Object Notation, is a text format commonly used to exchange structured data between applications. It is readable by people and straightforward for programs to parse, which makes it a frequent part of API responses, configuration files, environment settings, logs, and test fixtures.
A JSON formatter changes compact or unevenly spaced input into an indented, easier-to-scan form. A JSON validator checks whether the input follows JSON syntax. Many tools perform both jobs, but they are not interchangeable: a formatter improves presentation, while a validator identifies whether the document can be parsed.
Formatting is most useful as the first step in a broader debugging process. Once the data is expanded, you can inspect nesting, compare repeated objects, locate suspicious values, and decide whether the problem is invalid JSON or incorrect application data. A valid document can still contain the wrong field name, an unexpected type, a missing property, or a value that fails a downstream schema.
Keep the distinction between JSON and JavaScript in mind. JSON does not support comments, functions, undefined values, or arbitrary expressions. Its strings and property names use double quotation marks, and its values must be strings, numbers, objects, arrays, true, false, or null.
Step-by-step workflow
1. Preserve the original input
Before changing anything, save the original response or file in a safe working location. If the data came from an API, record the request context separately, including the endpoint, method, relevant parameters, and whether the response was expected to be an object or an array. Avoid pasting credentials, access tokens, personal information, or production secrets into an online utility.
Work on a copy when the payload may be needed for comparison. This makes it easier to distinguish a formatting change from a data change.
2. Run a validator before editing
Paste the copied text into a trusted JSON validator or use a parser in your editor or development environment. Start with validation rather than manually scanning the entire payload. A useful error message usually includes a line, column, or character position, although the reported location may be just after the actual mistake.
If the validator reports an error near the end of a large object, inspect the preceding property, comma, bracket, or quote first. Parsers often discover that the structure is impossible only after reading several additional characters.
3. Format the valid document
Once the input parses, use a JSON formatter to pretty print it with consistent indentation. Choose an indentation style that matches the surrounding project, such as two or four spaces, and avoid changing key ordering unless there is a clear reason. Consistent formatting makes code review and before-and-after comparisons more reliable.
For a command-line workflow in JavaScript, a small script can parse and print a file:
const fs = require('node:fs');
const input = fs.readFileSync('data.json', 'utf8');
const value = JSON.parse(input);
console.log(JSON.stringify(value, null, 2));This script intentionally stops when parsing fails. That is preferable to silently writing a partially corrected file.
4. Check the common syntax failures
Review the error location and check these patterns:
- Single quotes:
{'name': 'Ada'}is JavaScript-like, but valid JSON requires{"name": "Ada"}. - Unquoted property names: Every object key must be enclosed in double quotes.
- Trailing commas: JSON does not allow a comma after the final item in an object or array.
- Missing commas: Adjacent properties or array items need commas between them.
- Mismatched delimiters: Every opening brace or bracket must close with the matching character.
- Unescaped characters: Double quotes inside a string need escaping, for example
"She said 'hello'". - Invalid values: Use
true,false, ornullin lowercase. Values such asundefinedandNaNare not JSON values.
5. Reduce the payload when the error is unclear
Large nested responses can hide a small structural mistake. Copy the suspected object or array into a separate file and remove unrelated branches. Validate the smaller sample, then add sections back until the failure returns. This binary-search-like approach is often faster than repeatedly reading a complete API response.
When a payload contains many repeated records, keep one representative record first. If the reduced sample is valid, compare the remaining records for a different type, an unescaped value, or an unexpected delimiter.
6. Validate meaning after syntax
After the document is valid JSON, check whether it matches the contract expected by the application. Confirm required keys, data types, allowed values, date representation, null handling, array shape, and nesting. For example, a valid value of "42" may still be wrong if the consuming code expects the number 42.
Tools and handoffs
Use the simplest tool that fits the stage of the workflow:
- Editor formatter and parser: Best for local files, configuration, and repeated edits. It keeps the data close to the code and can often show the problem inline.
- JSON formatter or validator online: Convenient for harmless sample payloads and quick inspection. Treat the input as public unless the tool's privacy and processing behavior are clear to your team.
- Command-line parser: Useful in scripts, build checks, and CI. It makes validation repeatable rather than dependent on a manual browser step.
- API client: Helpful when the response must be inspected alongside request headers, parameters, status codes, and raw response content.
- Schema validation: Appropriate when syntax checks are not enough and a service needs consistent fields and types.
Make handoffs explicit. Share the smallest reproducible payload, the parser error, the expected structure, and the transformation that produced the data. Do not send a formatted copy without explaining whether formatting was the only change. In a repository, add a validation command to the normal development or CI workflow where malformed JSON could break a build or deployment. The CI/CD pipeline checklist provides a broader framework for deciding which checks belong before production deployment.
For JavaScript projects, keep the package manager and formatting commands consistent with the project configuration. Teams reviewing dependency or build changes may also benefit from the comparisons of npm, pnpm, and Yarn and common JavaScript build tools.
Quality checks
A clean formatter result is not the end of the review. Use this checklist before committing or forwarding a payload:
- The document parses without an error.
- Objects use quoted keys, double-quoted strings, and no trailing commas.
- Opening and closing braces and brackets are balanced.
- Numbers, booleans, nulls, strings, arrays, and objects use the intended types.
- Required fields are present and spelled correctly.
- Arrays contain the expected kind of item, especially when records are repeated.
- Empty, missing, and null values are treated according to the application contract.
- Unexpected secrets or personal data have been removed before sharing.
- The formatted output has been compared with the original to confirm that only presentation changed.
Also check the source of the problem. If an API returns malformed JSON, the defect may be in server-side string construction, a proxy response, an incorrect content type, or an error page being returned where JSON was expected. Inspect the HTTP status and raw response rather than assuming every response body is JSON. If the API returns valid JSON but the application still fails, move from syntax debugging to schema, business-rule, or integration debugging.
When to revisit
Revisit this workflow whenever a team changes an API contract, introduces a new serializer, modifies configuration generation, or adds a new service that exchanges JSON. Tool behavior and editor integrations can also change, so periodically confirm that the formatter, parser, and CI check produce the same result locally and in automated environments.
Make the process practical by keeping a small, non-sensitive set of representative fixtures: a minimal valid object, a nested response, an empty array, a null-containing response, and deliberately invalid examples for tests. Run them through the chosen validator after tool or dependency updates. When an incident occurs, add a reduced reproduction to the fixture set if it represents a failure the team could encounter again.
For your next JSON error, follow this order: preserve the input, validate it, format the valid copy, inspect the reported location, reduce the payload, and then check the data contract. That sequence turns a vague “JSON parse error” into a bounded debugging task while keeping sensitive data and production changes under control.