Blog · Developer & Web

How to Format and Validate JSON for APIs (With Examples)

Make your JSON API-ready: proper formatting, syntax validation, and common pitfalls that break requests. Free JSON formatter and validator in your browser.

Share
On this page

Working with APIs means working with JSON. Whether you're debugging a failing request, building a payload to send, or reviewing a response, properly formatted and validated JSON saves hours of debugging. This guide covers what matters when preparing JSON for APIs, the most common mistakes that cause errors, and how to catch them before they hit your endpoint.

Why JSON formatting matters for APIs

A correctly structured JSON document isn't just easier to read—it's what makes programmatic processing reliable. APIs parse JSON strictly according to the specification. A single misplaced comma, an unquoted key, or a stray comment can cause a 400 Bad Request before your data ever reaches the server's business logic.

There's also a collaboration angle. When you share a payload with a teammate or paste it into API documentation, properly indented JSON with consistent structure is far easier to review than a minified single-line blob.

The JSON specification: what's actually required

JSON has a small number of hard rules that trip people up constantly:

Keys must be strings in double quotes. { name: "Alice" } is not valid JSON. { "name": "Alice" } is. This is the single most common error for developers coming from JavaScript, where unquoted object keys are legal.

Strings use double quotes only. Single quotes are JavaScript, not JSON. { "city": 'London' } is invalid.

No trailing commas. { "a": 1, "b": 2, } is invalid because of the comma after 2. JavaScript (ES5+) allows trailing commas; JSON does not.

No comments. // this is not allowed and /* neither is this */ will break your JSON. If you need to annotate a payload, strip comments before sending.

Numbers don't need quotes. { "count": "42" } sends a string. { "count": 42 } sends a number. Many APIs treat these differently in validation and database storage.

Common JSON errors that break API requests

Unescaped special characters in strings

If a string value contains a double quote or a backslash, it must be escaped:

{ "message": "He said \"hello\"" }
{ "path": "C:\\Users\\Admin\\file.txt" }

Forgetting escapes is easy to miss in long payloads and causes cryptic parse errors.

Missing Content-Type header

Even perfectly valid JSON will fail if the server expects Content-Type: application/json and receives text/plain. Most HTTP libraries set this automatically when you serialize an object, but raw curl requests or custom implementations often miss it.

Sending a JSON string instead of parsed JSON

In JavaScript, JSON.stringify(obj) produces a string. If you accidentally double-stringify—calling it on a string that's already serialized—you end up sending an escaped string representation instead of an object. The server receives a valid JSON string, but the shape is wrong.

Nested objects as strings

{ "metadata": "{\"key\": \"value\"}" }

This sends metadata as a JSON string, not as a nested object. If the API expects a nested object, the request will fail validation. The fix is to pass the actual object:

{ "metadata": { "key": "value" } }

How to format JSON properly before sending

A well-formatted JSON payload for API work should use consistent two or four space indentation, keep keys in a logical order, and use the correct data types throughout. Here's an example of a clean API request body:

{
  "user": {
    "id": 42,
    "email": "alice@example.com",
    "active": true
  },
  "preferences": {
    "language": "en",
    "notifications": ["email", "sms"]
  },
  "timestamp": "2026-03-01T10:00:00Z"
}

Notice: all keys are double-quoted strings, values use the correct types (number for id, boolean for active, array for notifications), and there are no trailing commas.

Minifying JSON for production payloads

Once your JSON is valid and correct, you'll often want to minify it for the actual API request—removing whitespace to reduce payload size. A 10 KB formatted payload might be 4 KB minified, which matters at scale when sending thousands of requests per minute.

Minified version of the above:

{"user":{"id":42,"email":"alice@example.com","active":true},"preferences":{"language":"en","notifications":["email","sms"]},"timestamp":"2026-03-01T10:00:00Z"}

Use a formatter for development and debugging, minify for production. FixTools JSON Formatter lets you toggle between both in a single click.

Validating before you send

The fastest debugging loop is: write payload → validate → send. A JSON validator checks syntax before you make the HTTP call, so you're not guessing whether a 400 error came from your JSON or from the API's business rules.

A good validator will:

  • Point to the exact line and character where the error is
  • Identify the type of error (unexpected token, missing comma, unterminated string)
  • Show a clean preview of the parsed structure

FixTools JSON Validator does all of this in your browser—paste your payload, and any errors appear immediately with the cursor position highlighted.

Converting JSON to other formats

Some APIs accept or return data in formats other than JSON. Common needs:

  • JSON to CSV: Useful when an API response needs to go into a spreadsheet for reporting
  • JSON to XML: Some legacy SOAP-based services require XML
  • JSON to YAML: CI/CD configuration files and Kubernetes manifests often use YAML

All of these conversions are available in the FixTools JSON tools suite. The converters handle nested objects and arrays, flattening or serializing them according to the target format's conventions.

Quick checklist before sending a JSON payload

  1. Validate the syntax with a JSON validator
  2. Confirm all keys are double-quoted strings
  3. Check that values use correct types (don't send numbers as strings)
  4. Escape any special characters in string values
  5. Verify the Content-Type: application/json header is set
  6. For production, minify the payload to reduce bandwidth

Following this checklist consistently eliminates the most common categories of API JSON errors before they cause problems.

Try it instantly

Use these free FixTools right in your browser. No sign-up, no uploads—your data stays private.

Frequently asked questions

  • What's the difference between formatting and validating JSON?

    Formatting adds indentation and whitespace to make JSON readable. Validating checks that the JSON is syntactically correct—proper braces, quoted keys, no trailing commas. You need valid JSON before formatting matters; an invalid file will cause parse errors no matter how it's indented.

  • Why does my API return a 400 error when I send JSON?

    The most common causes are: missing or incorrect Content-Type header (should be application/json), invalid JSON syntax (trailing comma, unquoted key, single quotes instead of double quotes), or the payload being sent as a string instead of parsed JSON. Run your payload through a validator first to rule out syntax errors.

  • Does JSON key order matter for APIs?

    Technically no—the JSON specification does not guarantee key order, and most APIs treat objects as unordered. However, some poorly designed APIs or signature-based authentication systems (like AWS) require keys in a specific order. Always check the API documentation if you get unexpected errors.

  • What's the maximum JSON size an API can handle?

    It depends on the API. Most REST APIs default to a body limit of 1–10 MB. If you're hitting size limits, consider pagination, compression (gzip), or restructuring your payload to send data in chunks.

  • Can I use single quotes in JSON?

    No. The JSON specification requires double quotes for all strings and key names. Single quotes are a common mistake that causes immediate parse errors. If you're copying from a JavaScript object literal, make sure to convert it to valid JSON first.

O. Kimani

Software Developer & Founder, FixTools

Building FixTools — a single destination for free, browser-based productivity tools. Every tool runs client-side: your files never leave your device.

About the author →

Related articles