If you've worked with REST APIs, backend services, configuration files, or JavaScript applications, you've almost certainly encountered JSON.
JSON (JavaScript Object Notation) has become one of the most common formats for exchanging structured data between systems.
It's lightweight, human-readable, and supported by virtually every modern programming language.
However, working with JSON isn't always as straightforward as it seems.
A missing comma, an unexpected data type, an incorrectly escaped character, or a deeply nested response can create debugging problems that take longer to resolve than expected.
In this tutorial, we'll explore how JSON works, how to validate and format it, common mistakes developers make, and how to improve your workflow.
1. Understanding JSON Structure
JSON represents structured information using objects, arrays, strings, numbers, booleans, and null values.
Consider a typical API response:
{
"success": true,
"user": {
"id": 1042,
"name": "Alex Morgan",
"email": "alex@example.com",
"roles": ["admin", "editor"]
},
"lastLogin": null
}
This is valid JSON.
It contains a top-level object, nested user information, an array of roles, and a nullable field.
Although the format is simple, real API responses may contain thousands of lines of nested data.
That's where formatting and validation become essential.
2. JSON Formatting vs. JSON Validation
These two operations are closely related but serve different purposes.
JSON formatting organizes data using indentation and line breaks, improving readability.
JSON validation determines whether the data follows JSON syntax rules.
For example:
{"name":"Alex","active":true,"roles":["admin","editor"]}
This JSON is valid but difficult to scan.
Formatted:
{
"name": "Alex",
"active": true,
"roles": [
"admin",
"editor"
]
}
Both representations contain the same information.
Now consider:
{
"name": "Alex",
"active": true,
}
This is invalid JSON because of the trailing comma.
A formatter shouldn't silently invent missing data or repair arbitrary syntax errors. Validation should identify the problem so the developer can correct it.
3. Validate JSON Using JavaScript
JavaScript provides a built-in method called JSON.parse().
You can use it to check whether a string contains valid JSON.
function validateJSON(input) {
try {
JSON.parse(input);
return { valid: true, error: null };
} catch (error) {
return {
valid: false,
error: error.message
};
}
}
console.log(validateJSON('{"name":"Alex"}'));
console.log(validateJSON('{"name":}'));
The first example returns a successful validation result.
The second identifies invalid syntax.
One important detail: successful parsing proves syntactic validity, not that the data matches the structure your application expects.
For schema validation, developers may use libraries such as Ajv or Zod.
4. Format JSON Programmatically
JavaScript also includes JSON.stringify().
The third argument controls indentation.
const user = {
id: 1042,
name: "Alex",
active: true
};
const formatted = JSON.stringify(user, null, 2);
console.log(formatted);
The resulting output uses two spaces per indentation level.
This is especially useful when inspecting API responses, writing debugging utilities, or preparing configuration files.
5. Common JSON Mistakes
Trailing commas
JSON does not permit trailing commas.
Incorrect:
{"name":"Alex",}
Correct:
{"name":"Alex"}
Single quotation marks
JSON strings and object property names must use double quotation marks.
Incorrect:
{'name':'Alex'}
Correct:
{"name":"Alex"}
Undefined values
JSON supports null, but not undefined.
Incorrect:
{"status": undefined}
Correct:
{"status":null}
Unexpected data types
An API might return a numeric identifier as a string:
{
"userId": "1042"
}
This JSON is syntactically valid, but application logic expecting a number may fail.
Developers should validate both syntax and expected data types.
6. Debugging Nested API Responses
Imagine an API returns a deeply nested response containing customers, orders, addresses, and payment statuses.
Reading a single-line response may be difficult.
A practical workflow is:
- Capture the response in your browser's developer tools.
- Format the JSON for readability.
- Validate its syntax if necessary.
- Examine nested objects and arrays.
- Compare the actual structure with the application's expected schema.
- Reproduce the issue using a minimal test case.
For sensitive production data, avoid pasting raw payloads into services whose data-handling behavior you haven't reviewed.
7. Browser-Based JSON Tools
Sometimes you don't need to write a script just to format a response.
Browser-based developer utilities can speed up routine inspection.
For example, Utilgo's developer toolkit includes:
- JSON Formatter
- JSON Validator
- Base64 Encoder and Decoder
- UUID Generator
- Unix Timestamp Converter
The JSON formatter and validator process the entered data locally in your browser, making them convenient for quick checks without sending the JSON payload to a remote processing server.
You can use these tools to inspect a sample response, verify syntax, and copy the formatted result into your development environment.
8. Best Practices for Production Applications
JSON validation should be part of your application's design rather than only a debugging step.
Some useful practices include:
- Validate incoming API requests.
- Validate expected response shapes.
- Use clear error messages.
- Avoid exposing sensitive production data in logs.
- Handle missing and nullable fields explicitly.
- Keep JSON structures consistent across API versions.
- Add automated tests for invalid input.
It's also worth remembering that valid JSON does not necessarily mean trustworthy or safe data.
Applications must still apply authorization, business rules, and appropriate security controls.
Final Thoughts
JSON is simple enough to learn quickly, but real-world API development requires more than understanding curly braces and arrays.
Formatting improves readability, syntax validation catches malformed input, and schema validation helps ensure that data matches application requirements.
Combining these practices can make debugging faster and APIs more reliable.
For quick browser-based checks, explore the free JSON and developer tools on Utilgo.
What tools do you normally use when debugging complex JSON responses?
Share your workflow in the comments.
This article is part of a developer productivity series featuring practical tools and techniques for everyday software development.
Top comments (0)