WikiPlus

Format API Responses: A JSON Debugging Guide

Debugging API integrations is a daily task for most developers, and JSON formatting is at the heart of it. An unformatted API response is a wall of characters; a formatted response is a readable data structure. Knowing how to quickly capture, format, and inspect an API response — whether from browser dev tools, Postman, curl, or your application logs — dramatically speeds up integration work. This guide covers the practical techniques for formatting and debugging JSON API responses across every common tool and context.

Formatting API Responses in Browser Developer Tools

Browser developer tools are the first place most developers inspect API responses, and modern browsers have good built-in JSON formatting. Chrome and Edge Network panel: Open DevTools (F12), click the Network tab, and trigger your API request (reload the page or perform the action). Click the request in the list. In the Response tab, if the server returns Content-Type: application/json, Chrome renders the JSON in a formatted, collapsible tree view. You can expand and collapse objects and arrays, search for specific keys, and see type information at a glance. Raw text is also available in the Response subtab. Firefox Network panel: Firefox has a slightly different but equally capable JSON viewer. The Response tab for JSON responses shows the formatted, collapsible tree view by default, with a Raw Data option. Firefox's JSON inspector includes a search function and displays the character count of the response. Safari Network panel: Safari's developer tools show formatted JSON in the Response tab for API requests. The implementation is more basic than Chrome or Firefox but functional for most use cases. Limitations of DevTools: DevTools shows the response for requests made by the current page. For API calls made directly from your application code running outside the browser (Node.js server, mobile app, command-line scripts), DevTools is not applicable. In those cases, logging or proxies are necessary. Pasting into a formatter: For API responses that you capture via logging or external tools and need to inspect quickly, copy the JSON string and paste it into WikiPlus JSON Formatter. The tool formats, syntax-highlights, and validates the JSON in seconds. This is the fastest method for responses from non-browser contexts. Capturing responses that are not formatted: Some APIs return Content-Type: text/plain or application/octet-stream for what is actually JSON content. DevTools will not auto-format these as JSON. Copy the response body and paste it into a formatter to view it as JSON.

Inspecting API Responses with Postman and curl

Postman and curl are the standard tools for making and inspecting API requests outside a browser context. Postman: Postman automatically formats JSON responses with syntax highlighting and a collapsible tree view when the response Content-Type is application/json. The Pretty tab shows formatted JSON; the Raw tab shows the exact response body text. Postman also shows response size, time, and status code prominently. For API development and testing, Postman is one of the most efficient environments for inspecting responses because it stores request history, allows parameterized requests, and shows responses immediately formatted. curl: curl is a command-line HTTP client. By default, curl outputs the raw response to stdout — minified JSON appears as a single line. To format curl's JSON output, pipe it to jq: curl https://api.example.com/data | jq . The jq . command formats (pretty-prints) the output. Combined with jq's query syntax, you can extract specific fields immediately: curl https://api.example.com/users | jq '.[0].email' shows only the email of the first user. For HTTPS with authentication: curl -H 'Authorization: Bearer YOUR_TOKEN' https://api.example.com/protected | jq . Capturing raw responses for analysis: curl -s -o response.json https://api.example.com/data saves the raw response to a file without printing it. You can then open response.json in VS Code or jq for detailed inspection. For APIs with error responses: curl -s -w '\nHTTP Status: %{http_code}' https://api.example.com/data | head -n-1 | jq . outputs the formatted JSON plus the HTTP status code on the final line. This is useful for quickly checking both the response body and status in one command.

Common API Response Issues and How to Debug Them

Certain problems appear repeatedly when working with API JSON responses. Recognizing the pattern speeds up diagnosis. Double-encoded JSON: The response looks like a JSON string containing a JSON string — {"data":"{\"key\":\"value\"}"}. The inner JSON string is escaped rather than being a nested object. This happens when a server serializes a JSON string that is already a serialized string. The fix is on the server side — the value should be stored and returned as an object, not as a pre-serialized string. Temporarily, clients can JSON.parse() the inner string, but this is a server bug that should be fixed at the source. Empty responses with 200 status: The API returns HTTP 200 but an empty body. This often happens when a server-side template renders nothing (an empty list case not handled), when a middleware strips the body, or when a caching layer returns a stale empty response. Check the Content-Length header — if it is 0 or absent, the body is intentionally empty. If Content-Length indicates bytes but the body is empty, there may be a truncation issue. HTML error pages instead of JSON: The request to an API endpoint returns HTML (an error page, a redirect target, or a maintenance page) with Content-Type: text/html. This usually means the URL is wrong (hitting a web server's 404 page), the request is hitting a proxy or load balancer error page rather than the API, or authentication is failing and returning a login page instead of a 401 JSON response. Always check the Content-Type header before trying to parse a response as JSON. Incorrect character encoding: Some APIs return UTF-8 JSON without declaring the encoding in the Content-Type header, or they return responses in Latin-1 or Windows-1252 encoding. Characters outside ASCII (accented letters, Chinese characters, emoji) will be corrupted. Check the Content-Type header for charset=utf-8 and verify that your HTTP client is treating the response as UTF-8. Versioning mismatches: If your application was built against an older version of an API and the API has changed its response structure, you may receive unexpected fields, missing fields, or changed types. Format the response and compare it to the documentation for the version you are targeting.

Automating API Response Validation in Your Development Workflow

Manual inspection of API responses is fine during initial development, but as an integration matures, automated validation saves time and catches regressions. Snapshot testing: In your test suite, capture the API response for a specific request, save it as a JSON fixture file, and assert that future responses match the same structure. This is called snapshot testing. When the response structure changes intentionally (you update the API), update the snapshot. When it changes unintentionally (a regression), the test fails. Libraries like Jest support snapshot testing for API responses with one-line setup. Schema assertion tests: More robust than snapshots, schema tests validate the response against a JSON Schema definition. Rather than asserting that the response equals a specific value, you assert that it conforms to a structure. Fields can have different values in each test run (current timestamps, generated IDs) while still passing the schema assertion. This makes schema tests more resilient than snapshot tests for dynamic APIs. Contract testing: Consumer-driven contract testing (using tools like Pact) verifies that the API provider fulfills the contract expected by each consumer. Consumers define the format they need; providers verify they can fulfill it. This is particularly valuable in microservice architectures where multiple services consume the same API. Monitoring response format in production: Tools like Datadog, New Relic, and custom middleware can log JSON schema validation results for production API traffic. A sudden increase in schema validation failures for responses indicates a deployment that broke the response structure. Setting up alerts on schema validation failure rates provides early warning of API regressions before clients start reporting problems. Investing in these automation patterns reduces the time you spend manually formatting and inspecting API responses. The browser formatter remains your fastest tool for ad-hoc debugging; automated validation handles the ongoing quality assurance.

Frequently Asked Questions

How do I format a JSON API response in Chrome without any extra tools?
Chrome automatically formats JSON responses in the DevTools Network panel when the response has Content-Type: application/json. Open DevTools (F12), go to the Network tab, trigger the request, click it, and open the Response tab. For JSON responses, Chrome shows a formatted, collapsible tree view. If the API returns JSON with a different Content-Type, Chrome shows raw text. To see the formatted version in that case, copy the response body and paste it into the WikiPlus JSON Formatter in another tab, or type the URL directly in the browser address bar — Chrome formats raw JSON responses when you navigate to a JSON endpoint directly.
Can I format a JWT token's JSON payload in a browser formatter?
Yes, but you need to decode the token first. A JWT consists of three base64url-encoded parts separated by dots: header, payload, and signature. The payload is the JSON part. To inspect it, split the token on dots, take the second part (the payload), and base64url-decode it. You can use an online JWT decoder, or decode manually using atob() in the browser console (replacing - with + and _ with / first for standard base64). The decoded string is a JSON object that you can paste into a formatter. WikiPlus also has a Base64 tool that can decode the token payload.
Why does my API return different JSON structure on different calls?
Variable response structure is a design issue. Common causes: conditional fields that appear only when they have values (problematic), fields that differ based on user permissions or request parameters (intentional polymorphism), different code paths that return different serializers, or API versioning bugs where some requests hit old code and others hit new code. To diagnose: make multiple identical requests and compare the formatted responses side by side using VS Code's diff view. If the structure varies across identical requests, there is a bug in the server code. If the structure varies based on request parameters, document the variants explicitly in your API schema.