The JSON Debugging Toolkit
1. Browser DevTools (Chrome / Edge / Firefox)
Network Tab — Inspect API Responses
The Network tab is the first place to check when an API returns unexpected JSON:
- Open DevTools (F12 or Cmd+Option+I)
- Go to the Network tab
- Filter by Fetch/XHR to see only API calls
- Trigger the request (click button, refresh page)
- Click the request → Preview tab for collapsible tree view
- Click Response tab for raw JSON text
- Check Headers tab for Content-Type and status code
Tip
Content-Type header. It should be application/json. If the server sends text/html, you might be getting an error page instead of JSON.Console — Logging and Inspecting JSON
1// Basic — shows collapsed object2console.log(data);34// Table view — great for arrays of objects5console.table(data.users);67// Group related logs8console.group('API Response');9console.log('Status:', response.status);10console.log('Data:', data);11console.log('User count:', data.users.length);12console.groupEnd();1314// Pretty-print in console15console.log(JSON.stringify(data, null, 2));1617// Copy to clipboard for pasting into a tool18copy(JSON.stringify(data, null, 2)); // Chrome DevTools onlySources Tab — Breakpoint on JSON.parse
To catch parse errors before they propagate:
- Go to Sources tab → Breakpoints panel
- Expand XHR/fetch Breakpoints
- Add a breakpoint matching your API URL pattern
- When the breakpoint hits, step through the response handling code
- Inspect the raw response text before
JSON.parse()
2. Postman / Insomnia
Testing API Responses
1// Verify response is valid JSON2pm.test("Response is JSON", () => {3 pm.response.to.have.header("Content-Type", /json/);4 pm.response.to.be.json;5});67// Check specific fields8pm.test("User has required fields", () => {9 const user = pm.response.json();10 pm.expect(user).to.have.property("id");11 pm.expect(user).to.have.property("name");12 pm.expect(user).to.have.property("email");13 pm.expect(user.name).to.be.a("string");14});1516// Validate against schema17const schema = {18 type: "object",19 required: ["id", "name", "email"],20 properties: {21 id: { type: "number" },22 name: { type: "string" },23 email: { type: "string", format: "email" },24 },25};26pm.test("Matches schema", () => {27 pm.expect(pm.response.json()).to.have.jsonSchema(schema);28});Debugging Tips in Postman
| Feature | How to Use |
|---|---|
| Pretty view | Body tab → Pretty → auto-formats JSON |
| Search in response | Ctrl+F in response body to find specific keys |
| Save as example | Save response as an example for documentation |
| Console | View → Show Postman Console for full request/response logs |
| Environments | Store API URLs, tokens as variables for easy switching |
| Collection runner | Run all requests in sequence — catch regressions |
3. VS Code JSON Debugging
Built-in JSON Validation
VS Code validates .json files automatically. To enable JSON Schema validation for specific files:
1{2 "json.schemas": [3 {4 "fileMatch": ["package.json"],5 "url": "https://json.schemastore.org/package.json"6 },7 {8 "fileMatch": ["tsconfig.json", "tsconfig.*.json"],9 "url": "https://json.schemastore.org/tsconfig.json"10 },11 {12 "fileMatch": ["src/config/*.json"],13 "schema": {14 "type": "object",15 "required": ["host", "port"],16 "properties": {17 "host": { "type": "string" },18 "port": { "type": "integer", "minimum": 1, "maximum": 65535 }19 }20 }21 }22 ]23}Useful Extensions
| Extension | What It Does |
|---|---|
| JSON Crack | Visualize JSON as an interactive tree/graph |
| Paste JSON as Code | Generate TypeScript interfaces from JSON |
| REST Client | Send HTTP requests from .http files, see JSON responses inline |
| Pretty JSON | Format/minify JSON with a keyboard shortcut |
| Error Lens | Shows JSON validation errors inline next to the line |
4. Command Line with jq
jq is the most powerful command-line JSON processor:
1# Pretty-print2echo '{"name":"Alice","age":30}' | jq .34# Extract a field5curl -s https://api.github.com/users/octocat | jq '.login'6# "octocat"78# Extract from nested object9curl -s https://api.github.com/users/octocat | jq '.login, .public_repos'1011# Filter an array12echo '[{"name":"Alice","active":true},{"name":"Bob","active":false}]' | jq '.[] | select(.active == true)'1314# Map over array — extract just names15echo '[{"name":"Alice"},{"name":"Bob"}]' | jq '[.[] | .name]'16# ["Alice", "Bob"]1718# Count array items19echo '[1,2,3,4,5]' | jq 'length'20# 52122# Get keys of an object23echo '{"a":1,"b":2,"c":3}' | jq 'keys'24# ["a", "b", "c"]2526# Compact output (no whitespace)27jq -c . data.jsonjq with APIs
1# List all repo names from GitHub API2curl -s "https://api.github.com/users/octocat/repos" | jq '.[].name'34# Get repos with more than 100 stars5curl -s "https://api.github.com/users/octocat/repos" | jq '[.[] | select(.stargazers_count > 100) | {name, stars: .stargazers_count}]'67# Extract specific fields and reformat8curl -s "https://api.example.com/users" | jq '.data[] | {id, fullName: (.firstName + " " + .lastName), email}'910# Save filtered output to a file11curl -s "https://api.example.com/data" | jq '.results' > filtered.json5. Debugging Common Scenarios
Scenario: 'Unexpected token < in JSON'
This usually means the server returned HTML (an error page) instead of JSON:
- Open DevTools → Network tab
- Find the failing request
- Check Response tab — if it starts with
<!DOCTYPE, it's HTML - Check the status code — 404 or 500 means the API endpoint is wrong or broken
- Check Content-Type header — should be
application/json
Scenario: 'undefined is not valid JSON'
This means JSON.parse() received undefined instead of a string:
1// Common cause: response body is empty (204 No Content)2const text = await response.text();3console.log('Raw response:', JSON.stringify(text)); // Debug: see exactly what was received45// Fix: check before parsing6if (text) {7 const data = JSON.parse(text);8} else {9 console.log('Empty response body');10}Scenario: JSON Has Wrong Shape
The JSON parses fine but has unexpected field names or structure:
1// Log the full structure to understand the shape2const data = await response.json();3console.log('Keys:', Object.keys(data));4console.log('Type:', typeof data);5console.log('Is array:', Array.isArray(data));6console.log('Full structure:', JSON.stringify(data, null, 2));78// Check if data is wrapped in an envelope9// API might return { data: { users: [...] } } instead of { users: [...] }10console.log('data.data?', data.data);Try It — Validate Suspicious JSON
Try It Yourself
Paste suspected broken JSON here to validate it