Best practices 9 min read 1,905 words By LaiUse Editorial Team Reviewed 28 October 2026

How to Tell If a JSON API Response Is Malformed

How to Tell If a JSON API Response Is Malformed: An HTTP status code tells you whether the server considered the request successful, but it does not tell…

Why this matters

An HTTP status code tells you whether the server considered the request successful, but it does not tell you whether the response body contains valid JSON. A server or proxy can return an HTML error page, login prompt, gateway notice, or application-generated error document with a 200 status. The request appears successful to many clients, yet any code attempting to parse the body as JSON fails.

JSON is stricter than it may appear. Object members and array elements must be separated by commas, object keys and string values must use straight double quotation marks, and the closing punctuation must match the opening punctuation. A trailing comma, a missing comma, or a typographic “smart quote” can make an otherwise familiar response impossible to parse. Valid JSON is also not necessarily the response the caller expected: it may contain an error object, an empty body, or a different data structure. The first question is therefore whether the body is syntactically JSON; only then is it useful to ask whether its contents are correct.

The process

  1. Preserve the raw response. Capture the exact body without allowing an editor, browser extension, log viewer, or chat interface to normalize it. In particular, keep the original quotation marks and punctuation until the defect has been identified. A formatter may make the problem easier to see, but it should not be treated as a repair mechanism.
  2. Open json-formatter and choose validate mode. The tool has three real modes: format, minify, and validate. Validation is the appropriate starting point because it addresses the central question directly: whether the pasted text can be parsed as JSON. The formatter also performs its processing locally in the browser according to its documented capabilities.
  3. Paste the response body into the formatter. Paste the text exactly as received, including any leading whitespace, HTML, or unusual quotation marks. Do not first convert the response into a JavaScript object or remove characters that look suspicious. That would hide the malformed bytes that the parser needs to see.
  4. Review the validation result. If parsing fails, json-formatter reports an error beginning with “Invalid JSON:”. Do not assume that the first visible punctuation mark is necessarily the only problem, and do not assume the error text identifies the exact character or line unless the tool actually shows that detail. Treat it as evidence that the body violates JSON syntax somewhere.
  5. Locate the specific failure pattern. Inspect the body for the common cases. A trailing comma appears immediately before a closing brace or bracket. A missing comma appears between two complete values, such as two adjacent object properties. A smart quote appears as a curled opening or closing quotation mark inside a key or string. If the body begins with a tag such as an HTML document or paragraph element, it is not JSON at all, even when its HTTP status was 200.
  6. Correct the source or create a corrected copy. Remove only the syntax that is actually invalid: add a missing separator, remove an extra separator, or replace typographic quotation marks with straight double quotes. If the body is an HTML error page, do not try to turn the tags into JSON. Inspect the server-side error instead, or confirm whether the endpoint returned the intended representation.
  7. Validate again, then use format mode if needed. Once the text passes validation, format mode can make the structure easier to inspect. This is useful for reading nested objects and arrays, but formatting is not a substitute for validation. Minify mode serves the opposite purpose: it removes whitespace from JSON that is already valid. Neither mode should be expected to turn malformed input into a trustworthy API response.
  8. Query valid JSON when you need to isolate a value. Open json-path-query, enter an expression in the field labeled JSONPath Expression, and place the JSON in the field labeled Paste your JSON. The tool also accepts .json files and supports drag-and-drop. A query can help you locate a particular field after syntax is sound, but it cannot extract data from a body that the JSON parser rejects. If the API returns a JWT rather than ordinary JSON, use jwt-decoder instead: its labeled sections are Header (Algorithm & Type), JWT Token, and Payload (Claims).

Worked example

Suppose an API response looks like this:

{
  "id": 42,
  "name": "Ada"
  "roles": ["admin", "editor"]
}

The body is visually plausible, but there is no comma between the name and roles members. Pasting it into json-formatter and choosing validate mode causes an “Invalid JSON:” result because the parser cannot complete the object. Adding the missing comma produces valid JSON:

{
  "id": 42,
  "name": "Ada",
  "roles": ["admin", "editor"]
}

Now suppose a later response contains a trailing comma:

{
  "status": "ok",
}

That response also fails validation. The comma after the final member is not allowed in this JSON object, so removing it fixes the syntax. A third response may look valid until its quotation marks are examined closely:

{
  "message": “Success”
}

The opening quotation mark before Success is a typographic quote rather than the required straight double quote. A parser will reject it. Replacing both smart quotes with straight quotes resolves this particular defect, provided there are no other syntax errors.

The most misleading case is different. A proxy might return a page beginning with <!DOCTYPE html> or <html>, while the HTTP response reports 200. Validation still fails because HTML is not JSON. Renaming the body to a .json file would not change its contents or make it valid. The real issue is that the endpoint returned the wrong representation. After the server is corrected, the JSON can be validated and queried. For example, in json-path-query, the expression $.roles[0] can isolate the first role from the corrected object.

Common mistakes

Limitations of this approach

Frequently asked questions

What is the safest way to test an API response? Validate the raw body before formatting or querying it. This keeps syntax errors visible and prevents you from accidentally changing the evidence while trying to clean it up.

Does a trailing comma make JSON invalid? Yes. A comma cannot immediately precede the closing brace of an object or the closing bracket of an array. Removing that extra comma is the appropriate correction when no other defect is present.

Can a missing comma be easy to miss? Yes, especially in a one-line payload or when a long value ends next to another key. A visual formatter can expose the boundary between members, but validation is what confirms whether the JSON parser accepts the complete result.

Why does an HTML page fail even when the server returns 200? HTTP status and content syntax are separate concerns. A 200 response can contain HTML, but HTML is not a valid JSON document. The formatter correctly treats the body as invalid JSON, and the endpoint or intermediary still needs to be checked.

Are straight and smart quotation marks interchangeable in JSON? No. JSON string delimiters require the straight double-quote character. Smart quotes must be replaced, along with any other malformed characters found during validation.

Once the response validates, should I use format or minify? Use format when you need to inspect the structure. Use minify when you need a compact representation of JSON that already parses correctly. Neither mode establishes that the payload satisfies an API contract.

Can json-path-query tell me which line of malformed JSON is wrong? Its documented errors distinguish problems with the JSONPath expression from failures while processing input, but the tool do not promise line-level diagnostics. Fix JSON syntax with json-formatter first, then use JSONPath to select the fields you need.

When should I use jwt-decoder? Use it when the response contains a JWT rather than an ordinary JSON document. It exposes the Header (Algorithm & Type), Payload (Claims), and token structure, while reporting malformed formats. It is not a general-purpose JSON validator or a substitute for cryptographic token verification.

The tools in this guide: json formatter · json path query · jwt decoder — each runs in your browser, so your data stays on your device.

Browse every guide · Read the blog · About LaiUse