Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can you tell whether an HTTP response contains JSON?

Quick answer

Inspect the response’s Content-Type header for a JSON media type, such as application/json, and use it as a guide before parsing. Check the HTTP status separately: success does not prove that the body is JSON. Parsing can still fail if the body is unexpected or invalid, so handle that possibility rather than treating the header as a guarantee. [S1][S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I get a successful response from an HTTP request. Does that mean I can read it as JSON?

Geez

No. A successful status and the body’s format answer different questions. The status does not establish that the returned representation is JSON. Inspect the response’s Content-Type header first. For a JSON response, a common value is application/json. The header is useful metadata and a clue about the representation, not proof that the body is valid JSON. [S1]

With browser JavaScript Fetch, check the status and header separately, then attempt JSON parsing only when the header indicates JSON. Be ready for parsing to fail if the response body is unexpected or invalid. [S2]

Follow-up question

What should I check in the header, and what if it does not look like JSON?

Geez

Read the response’s Content-Type value and look for a JSON media type, commonly application/json. Treat that as guidance for choosing how to read the body. If the value indicates another format, do not automatically call a JSON parser: handle the response according to the format your application expects. The header describes the representation, but it cannot by itself guarantee that the body matches or is valid. [S1]

For example, if an endpoint is expected to return JSON but the response says text/html, pause before parsing it as JSON. That is a hypothetical example, not a claim about what any particular server returns. Check that you are examining the intended response and record the status, header, and a safe description of the body when investigating.

Follow-up question

Can you show a small Fetch example that checks the status and content type?

Geez

Here is an illustrative pattern for browser JavaScript. It checks the status separately, uses the header to decide whether to try JSON parsing, and handles a parse failure:

const response = await fetch("/api/example");
const contentType = response.headers.get("Content-Type") ?? "";

// Check response.status separately for your application.
if (!contentType.includes("application/json")) {
  throw new Error(`Expected JSON; received ${contentType || "no Content-Type"}`);
}

try {
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error("The response could not be parsed as JSON", error);
}

The application/json check is a simple example; adjust it to the response format your application expects. A matching header is a parsing clue, not a guarantee that parsing will succeed. Fetch separates checking the response status from reading its body, and its guidance supports checking Content-Type before trying to read JSON. [S1][S2]

Follow-up question

What should I write down when the header and the body seem to disagree?

Geez

Capture the status, the exact Content-Type value (or note that it is absent), and what happened when parsing was attempted. If you can inspect the body, describe whether it appears to be JSON or something else, but avoid sharing secrets or personal data from it. Then compare those observations with the response format your application expects.

A successful status, a JSON-looking header, or a JSON-looking sample alone does not establish that the entire body is valid JSON. The header is a clue for choosing how to parse; unexpected or invalid bodies still need to be handled. These observations do not by themselves explain why a mismatch occurred or what a particular server should return. [S1][S2]

A practical checklist

  • Check the HTTP status separately from the body format.

  • Read the response’s Content-Type header; look for a JSON media type such as application/json.

  • Use the header to guide how you read the body, not as a guarantee that parsing will succeed.

  • Handle unexpected formats and JSON parsing failures explicitly.

  • When troubleshooting, note the status, header value, and a safe description of the body.

A few more useful answers

Can a response have a successful status but not contain JSON?

Yes. A successful status alone does not prove the body is JSON. Check Content-Type and handle the body according to the format you expect. [S1]

Does application/json in Content-Type guarantee valid JSON?

No. Content-Type describes the representation and helps guide parsing, but an unexpected or invalid body can still require error handling. [S1]

Should I check Content-Type when using Fetch?

Yes. In browser JavaScript using Fetch, check the response header before trying to read the body as JSON, and handle status and parsing separately. [S2]

Sources

References used for this guide.

  1. [S1] Content-Type header — HTTP | MDN
    developer.mozilla.org · Checked
  2. [S2] Using the Fetch API — Web APIs | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work